Can someone help explain this diagram

Question:

((lambda (x y) (x y)) (lambda (x) (* x x)) (* 3 3))

This was No. 1 in the medium term, I put “81 9”, he thought I forgot to cross one of the rules, so I crossed out 81 and he left. In any case, I do not understand why it is 81.

I understand why (lambda (x) (* x x)) (* 3 3) = 81, but the first lambda I do not understand what the values ​​of x and y are, and what it does [body] (x y).

So, I was hoping that someone could explain to me why the first part does not look as if she is doing something.

+3
source share
4 answers

This requires a few indentation to clarify

((lambda (x y) (x y))
 (lambda (x) (* x x))
 (* 3 3))
  • (lambda (x y) (x y)); call xwith yas soon as parameter.
  • (lambda (x) (* x x)); evaluate the square of its parameter.
  • (* 3 3); rate to 9

, : " 9 ".

EDIT:

((lambda (x) (* x x))
 (* 3 3))

, - , .

+9

...

((lambda (x y) (x y)) (lambda (x) (* x x)) (* 3 3))

, . . () :

(lambda (x y) (x y))

:

(lambda (x) (* x x))

, .

(* 3 3)

, .

(lambda (x y) (x y))

Lambda , , : x y, x y ( , x y). -1.

(lambda (x) (* x x))

, . .

(* 3 3)

, , 9.

, :

(call-1 square 9)

, call-1 9. call-1 :

(square 9)

, call-1, . 9 81, .

+6

, Common Lisp :

((lambda (x y) (funcall x y)) (lambda (x) (* x x)) (* 3 3))

:

(funcall (lambda (x y) (funcall x y))
         (lambda (x) (* x x))
         (* 3 3))

Indeed, this first lambda does nothing useful, as it boils down to:

(funcall (lambda (x) (* x x)) (* 3 3))

which is equal

(let ((x (* 3 3)))
  (* x x))

equally

(let ((x 9))
  (* x x))

equally

(* 9 9)

equal to 81.

+2
source

The answers posted so far are good, so instead of duplicating what they already said, maybe this is another way to watch the program:

(define (square x) (* x x))

(define (call-with arg fun) (fun arg))

(call-with (* 3 3) square)

Still looking weird?

+1
source

Source: https://habr.com/ru/post/1698539/


All Articles