Search code examples
schemeracketbrackets

output of map function?


Hi I am trying to understand the output of the following code

(define p (lambda (x) (lambda (y) (x (x y)))))
(define q (lambda (x) (* x x))) 

when I use

(map (p q) (list 1 2 3)) 

the result is

(1 16 81)

shouldn't the answer be

(1 4 9) ?

Solution

  • You're mapping (p q) over the list, so start with figuring out what that is.

    Using the subsitution method, you get

        (p q)
    ==> ((lambda (x) (lambda (y) (x (x y)))) q)
    ==> (lambda (y) (q (q y)))
    ==> (lambda (y) (q ((lambda (x) (* x x)) y)))
    ==> (lambda (y) (q (* y y)))
    ==> (lambda (y) ((lambda (x) (* x x)) (* y y)))
    ==> (lambda (y) (* (* y y) (* y y)))
    

    so (p q) is a function that takes a number and squares its square.