E N D
1. Difference…Recursion/Iteration Recursive functions
You use the arguments to reduce something or count
You use a predicate to stop the recursion
You generally ‘unwind’ the recursive calls to get what you want
Iteration functions
You set up counters/temporary lists using let
You have to indicate the variable that is returned
You have to build lists (or count something) and then setq or setf these lists/or counts
2. And now…For all the other functions…..
3. I/O Input
(read) – reads from the terminal
(read-line…
(read-char
Output
(print …”this is the time for all good men”)
(print ‘hi_there)
(princ ….doesn’t put a line…
4. Examples Getting input from screen, & using this to
(defun interaction ()
(print “Enter a number from 1 to 10”)
(setq num (read))
(+ num 5))
5. Print Examples (defun printer (x)
(print ‘hello)
(print “hello there person”)
(princ ‘hi_there)
(print x))
* I probably use print for debugging programs more than anything else.
6. Functions as Arguments –And the power of Mapping functions
7. Funcall– Very Interesting! (funcall function A1..An)
Applies the function to arguments A1…An
The number of arguments must be known in advance – but there are ways to get around that….
Why in the heck would someone want to use this?
Examples
(setf funlist '(+ - *))
(funcall (car funlist) 6 2) returns 8
(funcall (car (cdr funlist)) 6 2) returns 4
(funcall (car (cdr (cdr funlist))) 6 2) returns 12
8. More Funcall… (FUNCALL <fn-name> <arg1> <arg2> ...)
>(FUNCALL ‘CONS ‘A ‘( B C ) )
(A B C)
> (FUNCALL ‘CAR ‘(A B C) )
A
> (SETQ OP ‘+)
+
> (FUNCALL OP 5 3 )
8
> (SETQ OP ‘-)
-
> (FUNCALL OP 5 3 )
9. APPLY Similar to funcall (but often used with lambda – explain in a minute)
The number of arguments do not need to be known in advance
(apply function arglist) OR apply function A1..An arglist)
Example
(setf a ‘(3 2 1))
(apply (function +) a) returns 6
(setf a (cons 10 a))
(apply (function +) a) returns 16
(apply (function +) 4 a) returns 20
10. A Better Example of APPLY Suppose we want to “do something’’ to an enemy identified, but the action is determined at run time (based on their actions, degree of threats, etc.).
(defun action (fn enemy)
(APPLY (function fn )(cons enemy nil) ) )
(defun seize (person) [put the person in jail, and…..... )
(defun kill (person) ... )
(defun drunk (person) ... )
> (action ‘seize ‘BlackKnight )
11. What if we want to apply a function to an entire list? (MAPCAR <fn-name> <arg1-list> <arg2-list>)
(this is lazy person’s recursion)…..
> (MAPCAR ‘+ ‘(1 2 3 4) ‘(100 200 300 400) )
(101 202 303 404)
> (SETQ action ‘drunk)
DRUNK
> (SETQ hostile-guests (identify-enemy guests))
> (MAPCAR action hostile-guests)
12. Lambda Allows creation of local, un-named functions
(lambda (Arg1..Arg2…) E1..En)
A1..An are a list of arguments
E1..En is a body of s-expressions
Example
(apply
(function (lambda (x y) ;This is the function
(+ (* x x)(* y y)) ) ) ;this is the function
‘(3 4) ) ; this is the list to that function
13. LAMBDA -- An anonymous function (LAMBDA (<arg1> <arg2> ...) <exp1> ....)
How to invoke it? It is typically invoked by EVAL, APPLY, FUNCALL, or mapping functions.
> (MAPCAR #’(lambda (x) (+ x bonus)) salary-list )
When to use it? It is typically used for defining a function that is needed only in a specific situation. Similar to information hiding….
Can it also appear in the position of a function? Yes, because LISP thinks LAMBDA forms are functions.
> ( (lambda (x) (cond ....... )) guest-list )
14. Mapcar with Lambda…. Example
(defun computer-speak (words)
(mapcar #’(lambda (word)
(cond ((equal word ‘I) ‘you)
((equal word ‘am) ‘are)
((equal word ‘happy) ‘sad)
(t word)))
words))
(computer-speak ‘(I am happy)) returns (you are sad)…
15. #’ An abbreviation for function – it tells the interpreter that a function is to follow
Example
(apply
#’(lambda (x y)
(+ (* x x)(* y y)) )
‘(3 4) )
16. Mapcar cont. With mapcar, you’re always going to give it a list
With mapcar, you’re always going to get a list back
There are other maps…maplist, mapcdr….etc.
17. Debugging The ? In debug mode, lists functions
Break [2]> ? Lists the functions available
Where
Up
Etc.
(trace <form>)
(untrace <form>)
(step <form>)
18. pprint To print a function when you are inside of lisp! (and boy is this hard to remember)
(pprint (symbol-function ‘<name of function>))
Example:
(pprint (symbol-function ‘test))
*note: Sometimes the print is “not quite” lisp-like.
19. Other famous lisp functions (assoc <item> <list of lists>
Association is used to retrieve list of second argument that has item as the first member of its list. Sort of like a database query…
(assoc ‘y ‘((x a) (y b) (x c)))
-> (y b)
20. Some or Every Mapping functions go until they run to completion…Sometimes you want to apply a function until you get a nil or t
(some <function name> <list>
(every <function name> <list>
Examples:
(some ‘numberp ‘(1 2 a b c))
t
(every ‘numberp ‘(1 2 a b c))
nil
** These two functions only return t or nil
21. First, second, third, …. (first list)
(second list)
(third list)
(fourth list)
….
(tenth list)
Returns that element of the list
22. Same thing (nth <number> list)
….
Does the same thing as (first….)
23. (ldiff list1 list2)
Returns a list of all elements in list1 that are different from lis2.
(set-difference list1 list2)
Returns the list of those elements of list2 that do not appear in list2
(union list1 list2 )
Returns list that is union of list1 and list2
(intersection list1 list2) Set functions
24. Style Use descriptive variable and function names
Don’t write C code in Lisp
Use Proper Indentation
S expressions all in one line
Indentation 3 spaces
Lining up your conds
Use white space appropriately
Example
25. Good:
(defun foo (x y)
(let ((z (+ x y 10)))
(* z z)))
Bad:
(defun foo (x y) (let ((z (+ x y 10))) (* z z)))
(defun foo ( x y )
(let ( ( z (+ x y 10 ) ) )
(* z z)
)
)
26. Don’t use line lengths greater than 80
Use cond instead of if for multiple test conditions
Make names of global variables begin and end with (*) ---Don’t use global variables
Use ‘() if you are using an empty list, use NIL if you are looking for a boolean. Use NULL to test for an empty list…NOT to test for a logical value.
Documentation – Use three semicolons in the left margin before the definition for major explanations. Use a single colon to explain something on the same line.
27. Exercises Write a lisp expression that creates the list (a b c) out of individual symbols a, b, and c using the list function.
Write a program that finds the smallest number in a list (find-smallest ‘(10 3 2 7))
28. Write a "guess the month you were born" game where the computer tries to guess the Month in a minimum number of guesses. First, the computer selects a month between January and December, prints it out, and prompts the user for an answer with please enter "Y" or "YES", "B" or "TOO BIG", "S" or "TOO SMALL". If the user enters "YES", the guess is correct, the game is over, and the computer prints the number of tries it made to get the correct month. If the month is earlier or later than the correct month, the computer makes another guess.
29. Example >(play-game)
I will try to guess the month in which you were born. Were you born in April?
If this is correct, please enter "Y" or "YES“
if "TOO LATE", enter "L" or
if "TOO EARLY", enter "E"
30. What is the data structure? (I don’t have arrays)
Let’s just do the game and see how we would do it….