Using LISP Functional Programming, respond to exercises?

Asked

Viewed 98 times

3

Suppose they were defined:

(defun xxx (x) (+ 1 x)) (setf xxx 5)

What is the value of the following expressions?

  1. (xxx 2)
  2. (xxx (+ (xxx 5) 3))
  3. (+ 4 xxx)
  4. (xxx xxx)
  • Why don’t you test this on a Isps interpreter and see the answer? Or do you want an explanation of the answer given?

  • Tip, when testing, use (print (xxx 2)), for example.

  • I’m learning LISP on my own and I’m not understanding very well I would like to understand even if it was with 1 example only the rest I turn around !

  • Check this site: http://rextester.com/l/common_lisp_online_compiler

  • It worked in this example of xxx 2 , gave 3 as a result !. I would like to understand the answer given .

1 answer

4


That defines from here xxx as a function that adds 1 to xxx:

(defun xxx (x) (+ 1 x))

That defines from here xxx as having the value 5:

(setf xxx 5)

LISP keeps values and functions separate. That is, you have a variable xxx with the value 5 and a function xxx adding up to one more.

  1. When you do that:

    (print (xxx 2))
    

    You are calling the function xxx and passing it 2 as parameter. The result is 3.

  2. Thereby:

    (print (xxx (+ (xxx 5) 3)))
    

    You are calling the function xxx and passing it 5 as parameter, resulting in 6. Then sum 3, which gives 9. Calls the function xxx again passing the 9, and gives 10.

  3. Already in that:

    (print (+ 4 xxx))
    

    The xxx is number 5. Adding with 4 is 9.

  4. Finally, this:

    (print (xxx xxx))
    

    You call the function xxx with the value of xxx (which is 5). Therefore, this results in 6.

See here working on rextester.

  • Thank you very much. Gave to understand well with your answer, helped quite . Grateful!

  • 1

    Until I learned now, @Gustavoaryel mark how you accept the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.