Re: Bug?

From: Lars Thomas Hansen <lth_at_ccs.neu.edu>
Date: Thu, 17 Oct 96 11:15:06 -0400

>Using snow 3.1 on my Linux box the following function produces
>different results each time I run it on (split1 (string->list
>"ab/cd"))
>
>(define (split1 lst)
> (cond ((null? lst) '(()))
> ((eq? (car lst) #\/) (cons () (split1 (cdr lst))))
> (else
> (let ((sf (split1 (cdr lst))))
> (set-car! sf (cons (car lst) (car sf)))
> sf))))
>
>If I replace "'(())" with "(list (list))" it works correctly.

I bet that if constants were read-only in STk it would deride you for
changing a constant: your set-car! changes the '(()) constant. To see
this, consider the following modification:

(define nullnull (list (list)))

(define (split1 lst)
  (cond ((null? lst) nullnull)
        ((eq? (car lst) #\/) (cons '() (split1 (cdr lst))))
        (else
         (let ((sf (split1 (cdr lst))))
           (set-car! sf (cons (car lst) (car sf)))
           sf))))

Then:

> (split1 (string->list "ab/cd"))
((#\a #\b) (#\c #\d))
> nullnull
((#\c #\d))
> (split1 (string->list "ab/cd"))
((#\a #\b) (#\c #\d #\c #\d))
> nullnull
((#\c #\d #\c #\d))

--lars
Received on Thu Oct 17 1996 - 17:16:29 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST