Lecture 1 Flashcards

1
Q

What is an atom?

A

letters, digits, *, -, , etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a List?

A

(1 2 3 4), (a (b c) (d (e f))) (sequence of atoms/lists)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what represents false in LISP?

A

nil and ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is an s-expression?

A
  1. 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how are s-expression lists evaluated?

A

first elements is function name and the others are arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how are number, lists and atomic symbols evaluated?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

write a function that squares a number

A

(defun square (x)

(* x x))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

explain defun arguments

A

first argument is name, second is a list of arguments passed in, third is the function itself (body)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

explain cond keyword

A

(cond (()())
(()()))
cond goes until non-nil value is found, if non is found returns nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how do we make a default action in cond statement?

A

last argument in cond should be (t x)
t - always true
x - default action

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

describe if statement

A

(if (

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

explain when and unless in terms of if statements

A

when and unless both return nil in else case
when - 1st arg is true preform second
unless - 1st arg is false preform secnd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is a predicate?

A

function that returns true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

when does lisp return nil?

A

to indicate false, t is used for EVERYTHING else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

explain and and or functions in lisp

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what does not do?

A

not takes and argument and returns the opposite

17
Q

what does list argument do?

A

takes and number of arguments and returns a list of those arguments

18
Q

what does cdr, car and cons do?

A

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

19
Q

what does append, nth and length do in lisp?

A

append - 2 args, combines them.
nth - 2 args (# and list), returns element at #
length - 1 arg, returns # of elements in list

20
Q

what does member, null and listp mean?

A

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

21
Q

what does atom do?

A

1 arg returns true if arg is atom. (atom (list 1 2 3 4)) = nil, (atom ()) = true

22
Q

explain quote, add it to (nth 0 (a b c d))

A

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

23
Q

what does eval do?

A

eval forces evaluation on something (opposite of quote)

24
Q

how do i initiate a variable?

A

setf - 2 args, binds second to first (setf x ‘(a b c))

25
Q

explain setf and defparameter? what does a change to defparameter mean?

A

setf- binds variables
defparameter - global variables variable-name
change to defparameter is considered a change to the program not a change by the program

26
Q

explain let

A

for local variables eg
(defun (x y)
(let ((a x) (b (* x y)))))
makes a = x and b = x*y and are local variables

27
Q

what does let* allow?

A

let* allows local variables to be used to initiate other variables eg. (let* ((a 2) (b (* a a)))), b is initiated with a * a

28
Q

write recursive factorial that takes 1 arg

A

(defun factrl (n)
(cond ((= n 0) 1)
(t (* n (factrl (- n 1))))))

29
Q

Explain scanning through a list recursivley (sum of #’s)

A
(defun sum (list)
(cond ((null list) 0)
(t (+ (first list)
(sum (rest list))))))
first = car, rest = cdr
30
Q

what is a first class object?

A
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)
31
Q

explain mapcar by first making a square function (explain #’)

A
#' - indicates argument is a function
(mapcar #'square '(1 2 3 4 5))
> (1 4 9 16 25)
32
Q

explain funcall

A

(setq list #’+)
(funcall list 1 2 3)
>6
funcall allows use function names that are already being used

33
Q

describe eval, funcall and apply to add(1 2 3)

A

(eval ‘(+ 1 2 3))
(funcall #’+ 1 2 3)
(apply #’+ ‘(1 2 3))

34
Q

explain lambda example use mapcar and square each and add 1

A

lambda creates temporary function with no name
(mapcar (lambda (x) (+ (* x x) 1))
‘(1 2 3 4))

35
Q

explain progn

A

evaluates a sequence of forms and returns value of last argument
forms - lists that are to be evaluated

36
Q

make a struture and explain it (person with first, last and age)

A

(defstruct person first last age)

setf p1 (make-person :first ‘fred :last ‘jones :age 18)

37
Q

explain dolist and dotimes

A

(dolist (m list) (print m)) - goes through list prints each

(dotimes (x 6) (print x)) - prints 0 1 2 3 4 5 NIL