LISP Introduction Lab

As always an unreadable or unclear answer is a wrong answer.

Expressions

Enter the following expressions into the clisp and write the results. If an expression is entered and nothing happens, check your parentheses!

1. (/ 10 (/ 10 (/ 10 2)))

2. (+ (* 3 12) 10)

3. (+ 2 4 6 8)

4. (setq x 12)

5. (setq y 20)

6. (* (- y x) (+ x y))

7. (setq z (+ x y))

8. z

9. (setq ans (+ z (* x 2)))

10. ans

11. (> 50 43)

12. (<= 3 53)

13. (setq one 5)

14. (setq two 10)

15. (= one 5)

16. (= two (* 2 one))

17. (setq name 'george)

18. (eq name 'george)

19. (eq name name)

20. (eq 'name 'george)

21. (and (= one 5) (eq? name 'george))

22. (or (= one 5) (eq? name 'george))

23. (not (= one 1))

Built-in Procedures  

1. (cons 'a '(b c))

(cons '(a) '(b c))

(cons 'a 'b)

(cons 'a (cons 'b '() ))

2. (equal (+ 4 5 6) (+ 6 5 4))

(equal 5 'five)

(equal (car '(a b c)) 'a)

3. (equal (+ 3 2) 5)

(equal '(1 2 3) '(1 (2 3)))

4. (null '())

(null '(1 2))

5. (car (cons 'a '(b c)))

(cdr (cons 'a '(b c)))

(cadr (cons 'a '(b c)))
 

Lists

A list data structure is central to Lisp as an abstract data type. A list is formed by enclosing any number of items within matching left and right parentheses. Write the resulting list for each expression shown below.

1. (setqone (cons 'a (cons 'b (cons 'c '()))))

2. (setq two (cons 'a (cons 'b 'c)))

  3. one

4. two

5. (setq rslt1 (car one))

6. (setq rslt2 (car two))

7. (setq rslt3 (caddr one))

8. (setq rslt4 (caddr two)) ----Why error??
 

9. (setq lst (cons (cons 'a '()) (cons 'b (cons 'c '()))))

10. (setq get-a (car (car (cdr '(b (a c) d)))))

11. (setq get-b (car (cdaddr '(z (y) (x b) (a)))))

12. (setq make-new (cons (car '(a b c)) (cdr '(1 2 3))))

13. (setq ans1 (equal (car '(a b)) (car (cdr '(b a)))))

14. (setq ans2 (equal (cons 'a '(b c))

(cons 'a (cons 'b (cons 'c '())))))
 
15.  (atom lst)

16. (atom (car lst))

17. (atom (caar lst))

  More Practice with Lists

Using the symbols one and two and the procedure cons, we can construct the list object

(one two) by typing (cons 'one (cons 'two '())) We can create the list object ((one) two) by typing (cons (cons 'one '()) (cons 'two '())) Using the symbols one, two, three, and four and the procedure cons, construct the following list objects. (one (two) three four)

(one (two three four))

(one (two three) four)

((one two) (three four))

For each of the following lists, write the expression using car and cdr that extracts the symbol a. You can use abbreviations like cadr, caar, etc. (b c a d)

((b a) (c d))

(((a)))

((d c) (a) b)
 
 

Cond

The most widely used LISP syntactic form for conditional execution is cond which has the following syntax:

(cond (<logical-test1> <exp1> <exp2> ... <expn>)
(<logical-test2> <exp1> <exp2> ... <expn>)
...
(else <exp1> <exp2> ... <expn>))
Each clause is evaluated in turn until a clause is encountered with a true logical-test, or until the else clause is encountered. When the first true clause is encountered, each associated expression is evaluated in left-to-right order. The result of the last expression evaluation is returned as the result of the cond.

(You may use lists from the above list section to fill in the following cond-construct.)

(setq get-smallest (cond ((< (car lst1) (car lst2)) (car lst1))
((< (car lst2) (car lst3)) (car lst2))
(T (car lst3))))
  Binding
 (defun getsmall (arg1 arg2 arg3)
    (cond
       ((< (car arg1) (car arg2)) (car arg1))
       ((< (car arg2) (car arg3)) (car arg2))
       (T (car arg3))))
  (getsmall lst1 lst2 lst3)