Ok, this is a fairly basic question: I am following the SICP videos, and I am a bit confused about the differences between define
, let
and set!
.
1) According to Sussman in the video, define
is allowed to attach a value to avariable only once (except when in the REPL), in particular two defines in line are not allowed. Yet Guile happily runs this code
(define a 1)
(define a 2)
(write a)
and outputs 2, as expected. Things are a little bit more complicated because if I try to do this (EDIT: after the above definitions)
(define a (1+ a))
I get an error, while
(set! a (1+ a))
is allowed. Still I don't think that this the only difference between set!
and define
: what is that I am missing?
2) The difference between define
and let
puzzles me even more. I know in theory let
is used to bind variables in local scope. Still, it seems to me that this works the same with define
, for instance I can replace
(define (f x)
(let ((a 1))
(+ a x)))
with
(define (g x)
(define a 1)
(+ a x))
and f
and g
work the same: in particular the variable a
is unbound outside g
as well.
The only way I can see this useful is that let
may have a shorter scope that the whole function definition. Still it seems to me that one can always add an anonymous function to create the necessary scope, and invoke it right away, much like one does in javascript. So, what is the real advantage of let
?
(+ a x)
in definition off
, as it's within the scope oflet
. – YasirA