Lecture 1 Flashcards
What is an atom?
letters, digits, *, -, , etc.
What is a List?
(1 2 3 4), (a (b c) (d (e f))) (sequence of atoms/lists)
what represents false in LISP?
nil and ()
what is an s-expression?
- an atom is an s-sxpression
2. if s⌄1, s⌄2, s⌄3, … s⌄n are s-expressions then so is the list (s⌄1 s⌄2 s⌄3)
how are s-expression lists evaluated?
first elements is function name and the others are arguments
how are number, lists and atomic symbols evaluated?
numbers - evaluated as numbers
atomic symbols - return value bound (error if nothing is bound)
list - evaluate second to last and apply the first element as function
write a function that squares a number
(defun square (x)
(* x x))
explain defun arguments
first argument is name, second is a list of arguments passed in, third is the function itself (body)
explain cond keyword
(cond (()())
(()()))
cond goes until non-nil value is found, if non is found returns nil
how do we make a default action in cond statement?
last argument in cond should be (t x)
t - always true
x - default action
describe if statement
(if (
explain when and unless in terms of if statements
when and unless both return nil in else case
when - 1st arg is true preform second
unless - 1st arg is false preform secnd
what is a predicate?
function that returns true or false
when does lisp return nil?
to indicate false, t is used for EVERYTHING else
explain and and or functions in lisp
and - everything has to be t, or result is nil. If everything is true last expression is returned.
or - evaluates until non-nil is found.
what does not do?
not takes and argument and returns the opposite
what does list argument do?
takes and number of arguments and returns a list of those arguments
what does cdr, car and cons do?
cdr - takes list and returns list with first element removed
car - 1 arg returns first element in list
cons - 2 args, returns list with first argument at start of list
what does append, nth and length do in lisp?
append - 2 args, combines them.
nth - 2 args (# and list), returns element at #
length - 1 arg, returns # of elements in list
what does member, null and listp mean?
member - 2 args, tries to find element in second arg. eg (member 2 (1 2 3 4)) = (2 3 4)
null - 1 arg, returns t if list is empty
listp - 1 arg, returns true if arg is list
what does atom do?
1 arg returns true if arg is atom. (atom (list 1 2 3 4)) = nil, (atom ()) = true
explain quote, add it to (nth 0 (a b c d))
this doesn’t work because a isnt a function so fails. (nth 0 quote(a b c d)) = does work, so does (nth 0 ‘(a b c d)). quote returns arg without evaluating
what does eval do?
eval forces evaluation on something (opposite of quote)
how do i initiate a variable?
setf - 2 args, binds second to first (setf x ‘(a b c))
explain setf and defparameter? what does a change to defparameter mean?
setf- binds variables
defparameter - global variables variable-name
change to defparameter is considered a change to the program not a change by the program
explain let
for local variables eg
(defun (x y)
(let ((a x) (b (* x y)))))
makes a = x and b = x*y and are local variables
what does let* allow?
let* allows local variables to be used to initiate other variables eg. (let* ((a 2) (b (* a a)))), b is initiated with a * a
write recursive factorial that takes 1 arg
(defun factrl (n)
(cond ((= n 0) 1)
(t (* n (factrl (- n 1))))))
Explain scanning through a list recursivley (sum of #’s)
(defun sum (list) (cond ((null list) 0) (t (+ (first list) (sum (rest list)))))) first = car, rest = cdr
what is a first class object?
named by a symbol, passed as an argument to a function. may be returned by a function, may be an element of a larger data structure (lists). lisp functions are first class objects (functions can take functions as parameters)
explain mapcar by first making a square function (explain #’)
#' - indicates argument is a function (mapcar #'square '(1 2 3 4 5)) > (1 4 9 16 25)
explain funcall
(setq list #’+)
(funcall list 1 2 3)
>6
funcall allows use function names that are already being used
describe eval, funcall and apply to add(1 2 3)
(eval ‘(+ 1 2 3))
(funcall #’+ 1 2 3)
(apply #’+ ‘(1 2 3))
explain lambda example use mapcar and square each and add 1
lambda creates temporary function with no name
(mapcar (lambda (x) (+ (* x x) 1))
‘(1 2 3 4))
explain progn
evaluates a sequence of forms and returns value of last argument
forms - lists that are to be evaluated
make a struture and explain it (person with first, last and age)
(defstruct person first last age)
setf p1 (make-person :first ‘fred :last ‘jones :age 18)
explain dolist and dotimes
(dolist (m list) (print m)) - goes through list prints each
(dotimes (x 6) (print x)) - prints 0 1 2 3 4 5 NIL