Lecture 1 Flashcards
(37 cards)
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))