Using LISP Functional Programming, answer the exercises?

4

Suppose you have defined:

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

What is the value of the following expressions?

  • (xxx 2)
  • (xxx (+ (xxx 5) 3))
  • (+ 4 xxx)
  • (xxx xxx)
  • asked by anonymous 13.10.2017 / 01:52

    1 answer

    5

    This defines xxx as a function that adds 1 to xxx :

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

    This defines xxx as having value 5:

    (setf xxx 5)
    

    LISP maintains separate values and functions. That is, you have a xxx variable with a value of 5 and a xxx function that adds one more.

  • When you do this:

    (print (xxx 2))
    

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

  • With this:

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

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

  • Already in this:

    (print (+ 4 xxx))
    

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

  • Finally, this:

    (print (xxx xxx))
    

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

  • See here working on rextester.

        
    13.10.2017 / 02:38