Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is (lambda lambda lambda)?

Tags:

racket

My friend shows this to me, and I am really curious why it works like this. I at first thought that it's gonna be a syntax error, but it doesn't... Here're some of my experiments:

> (lambda lambda lambda)
#<procedure>
> ((lambda lambda lambda))
'()
> ((lambda lambda lambda) 1 2 3 4 5 6 7)
'(1 2 3 4 5 6 7)
> (lambda lambda foo)
#<procedure>
> ((lambda lambda foo))
foo: undefined;
 cannot reference an identifier before its definition
> (lambda lambda 1 2 3 4 5)
#<procedure>
> ((lambda lambda 1 2 3 4 5))
5
> (lambda foo lambda)
. lambda: bad syntax in: lambda
> (lambda 1 2 3)
. lambda: bad argument sequence in: 1
> ((lambda) 1 2 3)
. lambda: bad syntax in: (lambda)

So it seems:

  1. In lambda, lambda could be arg-ids?
  2. In lambda, lambda could be a list constructor?
like image 853
Sorawee Porncharoenwase Avatar asked Feb 16 '16 07:02

Sorawee Porncharoenwase


1 Answers

Ohhh I got it. lambda could be shadowed!

> ((lambda (lambda) (+ 1 lambda)) 7)
8

Also, the syntax given at https://docs.racket-lang.org/guide/lambda.html is not entirely correct because at arg-ids position, an identifier could be there! This will bind the list of arguments with the identifier:

> ((lambda foo foo) 1 2 3 4)
'(1 2 3 4)

These explain it!

like image 184
Sorawee Porncharoenwase Avatar answered Oct 14 '22 17:10

Sorawee Porncharoenwase