[plt-scheme] Side effect requiring rnrs/base-6?
[sent privately too by accident]
2009/9/16 Ganesh Gunasegaran <ganesh.gunas at gmail.com>:
> Hi All,
>
> I am very new to Scheme
>
> What is wrong with the following code?
>
> --------------------------------------------------------------------------------------------------------------------------------
> #lang scheme
> (require rnrs/base-6)
> (letrec ([my-sum (lambda (l)
> (if (null? l)
> 0
> (+ (car l) (my-sum (cdr l)))))])
> (my-sum '(1 2 3 4)))
> (define my-sum
> (lambda (l)
> (if (null? l)
> 0
> (+ (car l) (my-sum (cdr l))))))
>
> OUTPUT
> Welcome to DrScheme, version 4.2.1 [3m].
> Language: Module; memory limit: 128 megabytes.
> 10
>> (my-sum '(1 2 3))
> . . mcar: expects argument of type <mutable-pair>; given (1 2 3)
> --------------------------------------------------------------------------------------------------------------------------------
> Am I missing something?
There are two concepts of pairs in PLT Scheme: immutable and mutable pairs.
The "scheme" language use immutable pairs, and the rnrs languages use
mutable ones.
Since the language chosen in this source file is Scheme, the expression '(1 2 3)
will expand to (quote (1 2 3)) which again will expand to
(immutable-cons 1 (immutable-cons 2 (immutable-cons 3 null))) .
Since functions such as mcar from rnrs expect mutable pairs, you'll
get an error.
Instead of '(1 2 3) try (mcons 1 (mcons 2 (mcons 3 null))).
After thought: Is there an "mutable quote" mquote available somewhere?
--
Jens Axel Søgaard