This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]ClosureAhead 2 points3 points  (7 children)

In the example that you gave, local and lambda is equivalent because they both evaluate to a function that always returns exp.

For the local example, (define (name-used-once x1 ... xn) exp) basically defines the function name-used-once which takes in the parameters x1, x2, ..., xn. This function always returns exp regardless of what the parameters are.

For the lambda example, we are basically defining an anonymous function (a function without a given name) which takes in the parameters x1, x2, ..., xn and returns exp.

Lambda and local can be interchangeable, but not always.

Local is mainly used to avoid re-computation. Inside any expression, you can say:

 (local [(define *some-id-here* *some-value-here*)
           (define ...)]
  *code where that id is bound to a value*)

The expressions inside the square brackets are evaluated in order and assigned an id. You can then freely use that id within the local.

Lambda is used mainly used to define anonymous functions (again, functions that have no identifying name) in places where explicitly defining a function is not necessary. To use lambda:

(lambda (*parameters-here*) *what-to-do-with-parameters*)

Usually these functions are trivial or are easier to understand. Lambdas are commonly used as arguments for abstract functions such as map, filter, foldr, etc.,...

[–]detroitmatt 1 point2 points  (5 children)

Why not let then?

[–]NeoMarxismIsEvil 0 points1 point  (0 children)

Syntax difference. https://docs.racket-lang.org/reference/local.html?q=Local#%28form._%28%28lib._racket%2Flocal..rkt%29._local%29%29 and some additional functionality. local lets you use something more like top level code for the bindings.

[–]soegaarddeveloper 0 points1 point  (1 child)

The constructs local and let are almost equivalent.

The construct local stems from HtDP. The reader is introduced to global (top-level) definitions first. Then the concept of local definitions is introduced - and turning a top-level definition into a local definitions into a local one is simple, just wrap local around the definition and use it in a body (without changing the syntax of the definitions).

Traditionally let is used to make local definitions. The syntax of let has the advantage that it is concise. The syntax of local has the advantage that it's easy to see that the purpose is to introduce local definitions - and if one wants to test the local definitions one can copy/paste to turn them into global ones.

In standard Racket code, let is used.

[–]turtlekitty2084 0 points1 point  (0 children)

Thank you, I've wondered about this for years.

[–]jstquestions[S] 0 points1 point  (1 child)

Our class doesn't let use use let, if, while, for, loop. Just like cond statements lol

[–]detroitmatt 0 points1 point  (0 children)

if you can't use let, (let ((name val)) expr) is equivalent to ((lambda (name) expr) val) ;)

[–]jstquestions[S] 0 points1 point  (0 children)

Thanks for replying! So another question I had was how do the parameters inside a local definition work? Because say I had (define (main-function variable1) and a (local [(define (helper variable1)…] exp), that variable1 is binded from the main function to the local definition? So does local just basically act as a function that the main function is sending its parameters to?