Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(list lambda lambda)

Tags:

syntax

scheme

What 'kind of thing' will I get if I do this?

(car (list lambda lambda))

I thought I'd get lambda back, which means I could do

(define my_lambda (car (list lambda lambda)))
(define foo (my_lambda (n) (+ n n)))

But that didn't work! Thanks,

like image 251
user1508893 Avatar asked Dec 10 '25 14:12

user1508893


2 Answers

lambda is a special form (meaning: standard evaluation rules don't apply to it), part of the core primitives of the language and is not a symbol or other kind of value that can be assigned to a variable.

Answering your question, the "kind of thing" that you'll get after evaluating the expression (list lambda) will depend on the Scheme interpreter that you're using, but more often than not you'll get an error. For instance, DrRacket complains like this:

lambda: bad syntax in: lambda
like image 167
Óscar López Avatar answered Dec 13 '25 18:12

Óscar López


In some sense, lambda doesn't exist at runtime (sure, the functions created by lambda statements exist, but that's a different matter; they aren't lambda itself).

The reason for this is that the lambda statement manipulates other things that don't exist at runtime; in particular, it changes the meaning of variable names (in your example, n).

To answer your question about what kind of thing lambda is, the usual answer is "syntax". Fortunately, Scheme provides a mechanism to abstract over syntax: macros. Macros can abstract over compile-time-only entities, like variable names and lambdas and other macros. So, you could write (in the Racket REPL, in this case):

> (define-syntax-rule (mylambda (x ...) body)
    (lambda (x ...) body))
> (define foo (mylambda (n) (+ n n)))
> (foo 71)
142

There are multiple systems for defining Scheme macros; the syntax-rules system uses ...s in an unusual, but ultimately pretty intuitive fashion. It is also possible to define macros by writing Scheme code that emits Scheme, which involves a little more complication.

like image 36
Paul Stansifer Avatar answered Dec 13 '25 17:12

Paul Stansifer