


Part 2 Summer 2014 Web.pdf
Part_2_Summer_2014_Web.pdf
Showing 2126 out of 53
Part 2 Summer 2014 Web.pdfPart II: Functional Pro...
Part_2_Summer_2014_Web.pdfPart II: Functional Programming with LISP
Part 2 Summer 2014 Web.pdfPart II:...
Part_2_Summer_2014_Web.pdfPart 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> <listtoiterateover>
<optionalreturnvalue>
)
<expression>
)
Page 22
22
43
Iterate over List
•
dolist
evaluates the listtoiterateover, 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
(newlist)
;initially nil:
(
let
(
(
newlist ‘()
)
)
(
dolist
(x lst)
(
setf
newlist (
cons
x newlist))
)
newlist
)
)
•
(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
•
We have already seen
cons
,
car
and
cdr
.
•
What
cons
really does is combine two objects into a
twopart 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
copylist
takes a list and returns a copy
of it. The new list will have the same elements but
contained in a new address:
(setf x ‘(a b c)
y (copylist x))
•
(defun ourcopylist (lst)
(if (atom lst) ;or (null lst)
lst ; or ‘(), but not (list ‘())
(cons (car lst) (ourcopylist (cdr lst)))))
a
c
b
x
a
c
b
y
Ace your assessments! Get Better Grades
Browse thousands of Study Materials & Solutions from your Favorite Schools
Concordia University
Concordia_University
School:
Principles_Of_Programming_Lang
Course:
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
Students also viewed documents
lab 18.docx
lab_18.docx
Course
Course
3
Module5QuizSTA2023.d...
Module5QuizSTA2023.docx.docx
Course
Course
10
Week 7 Test Math302....
Week_7_Test_Math302.docx.docx
Course
Course
30
Chapter 1 Assigment ...
Chapter_1_Assigment_Questions.docx.docx
Course
Course
5
Week 4 tests.docx.do...
Week_4_tests.docx.docx
Course
Course
23
Week 6 tests.docx.do...
Week_6_tests.docx.docx
Course
Course
106