Part 2 Summer 2014 Web.pdf-Part II: Func...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
Showing 21-26 out of 53
Part 2 Summer 2014 Web.pdf-Part II:...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
##### Page 21
21
41
Counted Loop
(
defun
abc (x y)
(
let
((result 1))
(
dotimes
(count y result)
(
setf
result (* x result))
)
)
)
(abc 2 3) ;Output: 8
abc is calculating x^y.
The value of dotimes expression is result.
42
Iterate over List
Iterating over a list with
dolist
:
(
dolist
(
<var> <list-to-iterate-over>
<optional-return-value>
)
<expression>
)

##### Page 22
22
43
Iterate over List
dolist
evaluates the list-to-iterate-over, then one by one sets
var
to each element in the list, and evaluates the expression.
dolist
then returns the optional return value, else nil if none is
provided
.
(
defun
abc (lst)
(
let
(new-list)
;initially nil:
(
let
(
(
new-list ‘()
)
)
(
dolist
(x lst)
(
setf
new-list (
cons
x new-list))
)
new-list
)
)
(abc '(a b c d e f g))
;Output: g f e d c b a
abc reverses the list it has as argument.
44
Functions as Objects
We can pass functions as arguments.
One function that takes a function as an argument is
apply
. It takes a function and a list of arguments for it
and returns the result of applying the function to the
arguments
:
(
apply function
+ ‘(1 2 3)) => 6
or
(
apply
#’+
‘(1 2 3)) => 6 ;shorthand notation
(
sort
(list 8 4 0 2 9 7)
#’<
) => (0 2 4 7 8 9)
(
mapcar
#’*
‘(2 3 7) ‘(5 6)) => (10 18)

##### Page 23
23
Lists
46
Some Operations
cons
,
car
and
cdr
.
What
cons
really does is combine two objects into a
two-part object.
Conceptually a
cons
is a pair of pointers; the first one is
the head of a list (accessible through
car
) and the
second one is the tail of the list (accessible through
cdr
).
e.g. (
setf
x (
cons
‘a
nil
))
(
car
x) => a
(
cdr
x) => nil
nil
a
a
or

##### Page 24
24
47
Example
(
setf
y (list ‘a ‘b ‘c))
(
cdr
y) => (b c)
a
b
c
48
a
d
b
c
Example
(
setf
z
(
list
‘a
(
list
‘b ‘c
)
‘d
)
)
;; Output: (a (b c) d)
This list has three elements. It just happens that the
second element of
z
is also a list.
z
is an example of a
nested
list (as opposed to a
flat
list)
(
car
(
cdr
z)) => (b c)

##### Page 25
25
49
Example
How to construct the list (a (b c) d e)
using cons?
(cons ‘a (cons (cons ‘b (con ‘c ‘()))
(cons ‘d (cons ‘e ‘()))))
50
Equality
Each time you call
cons
, LISP allocates a new piece of
memory with room for two pointers.
If we call
cons
twice with the same arguments, we get
back two values that look the same but are in fact
distinct objects.
(
eql
(
cons
‘a
nil
) (
cons
‘a
nil
))
nil
(
setf
x (
cons
‘a
nil
))
(
eql
x x) => T
(
equal
x (
cons
‘a
nil
)) => T

##### Page 26
26
51
Equality
Every value is conceptually a pointer.
(
setf
x '(a b c))
(
setf
y
x)
(
eql
x y) => T
a
c
b
x
y
52
Building Lists
The function
copy-list
takes a list and returns a copy
of it. The new list will have the same elements but
(setf x ‘(a b c)
y (copy-list x))
(defun our-copy-list (lst)
(if (atom lst) ;or (null lst)
lst ; or ‘(), but not (list ‘())
(cons (car lst) (our-copy-list (cdr lst)))))
a
c
b
x
a
c
b
y

Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Principles_Of_Programming_Lang
Course:
Great resource for chem class. Had all the past labs and assignments
Leland P.
Santa Clara University
Introducing Study Plan
Using AI Tools to Help you understand and remember your course concepts better and faster than any other resource.
Find the best videos to learn every concept in that course from Youtube and Tiktok without searching.
Save All Relavent Videos & Materials and access anytime and anywhere
Prepare Smart and Guarantee better grades