Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does `let` work in Scheme?

Tags:

scheme

I use let to create a temporary variable, and then use this temporary variable in the next statement. However, DrScheme complained,

let: bad syntax (not an identifier and expression for a binding) in: temp

This is my code snippet:

(define (case-one-helper str)
  (let (temp (substring str (+ 3 (string-contains str "my"))))
    (substring temp (string-contains temp " "))))

I wonder if the value of variable created by let has to be known at compiled time?

Edit I've just figured out, missing ().

Thanks,

like image 740
Chan Avatar asked Apr 23 '11 20:04

Chan


People also ask

How let and Letrec constructs work in Scheme?

In a let expression, the initial values are computed before any of the variables become bound; in a let* expression, the bindings and evaluations are performed sequentially; while in a letrec expression, all the bindings are in effect while their initial values are being computed, thus allowing mutually recursive ...

What is let * In racket?

Local Binding: let, let*, letrec, ... in The Racket Reference also documents let. A let form binds a set of identifiers, each to the result of some expression, for use in the let body: (let ([id expr] ...) body ...+)

How does let work in Lisp?

The let expression is a special form in Lisp that you will need to use in most function definitions. let is used to attach or bind a symbol to a value in such a way that the Lisp interpreter will not confuse the variable with a variable of the same name that is not part of the function.

What are let and let * forms in Lisp?

let and let* create new variable bindings and execute a series of forms that use these bindings. let performs the bindings in parallel and let* does them sequentially. first evaluates the expressions init-form-1, init-form-2, and so on, in that order, saving the resulting values.


2 Answers

While not exactly the problem you're experiencing, but an aside based on your questioning about the sequence of evaluating the arguments, let is also "syntactic sugar" for a lambda followed by it's arguments that are first evaluated and then passed to the lambda which is then evaluated.

For instance:

(let ((a (list 1 2 3))
      (b (list 4 5 6)))
     (cons a b))

is the same as:

((lambda (list-a list-b) (cons list-a list-b)) (list 1 2 3) (list 4 5 6))

So, if you're ever wondering about evaluation sequence, the arguments are evaluated fully before the body is evaluated (and one argument cannot refer to an argument preceding it ... use let* for something that requires bindings like that).

like image 176
Jason Avatar answered Sep 21 '22 14:09

Jason


You need to put another set of parentheses around your let declarations:

(define (case-one-helper str)
  (let ((temp (substring str (+ 3 (string-contains str "my")))))
    (substring temp (string-contains temp " "))))
like image 24
Rachel Shallit Avatar answered Sep 18 '22 14:09

Rachel Shallit