Part 2 Summer 2014 Web.pdf-Part II: Func...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
Showing 47-51 out of 53
Part 2 Summer 2014 Web.pdf-Part II: Functional Pro...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
Part 2 Summer 2014 Web.pdf-Part II:...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
Page 47
47
93
Example: list-append
(defun list-append (L1 L2)
(if (null L1)
L2
(cons (car L1) (list-append (cdr L1) L2))))
94
Set Operations
Set theory: a set is different from a list
Operations:
Membership
Union
Intersection
Difference
Symmetric Difference


Page 48
48
95
Some Set Operations: Membership
(defun is-member? (element lst)
(cond
((null lst)
'())
((equal element (car lst))
t)
(t
(is-member? element (cdr lst)))))
> (is-member? 'a '())
nil
> (is-member? 'a '(a b c d))
true
> (is-member? '(a b) '(a (a b) b))
true
96
Some Set Operations: Intersection
(defun my-intersection (lst1 lst2)
(cond
((null lst1) '())
;((null lst2) '())
((member (car lst1) lst2)
(cons (car lst1)
(my-intersection (cdr lst1) lst2)))
(t (my-intersection (cdr lst1) lst2))))
> (my-intersection '(a b) '(a b c))
(list 'a 'b)
> (my-intersection '(a b c) ‘())
nil


Page 49
49
97
Some Set Operations: Difference
(defun setdiff (lst1 lst2)
(cond
((null lst1) '())
((null lst2) lst1)
((member (car lst1) lst2)
(setdiff (cdr lst1) lst2))
(t (cons (car lst1)
(setdiff (cdr lst1) lst2)))))
> (setdiff '(a b c) '(c d e))
(list 'a 'b)
> (setdiff '() '(a b c))
nil
> (setdiff '(a b c d e f g) '(a b c d e f))
(list 'g)
98
(defun my-union (lst1 lst2)
(cond
((null lst1) lst2)
((null lst2) lst1)
((member (car lst1) lst2)
(cons (car lst1)
(my-union (cdr lst1)
(setdiff lst2
(cons (car lst1) '())))))
(t (cons (car lst1) (my-union (cdr lst1)
lst2)))))
> (my-union '(a b c) '(d e f))
(list 'a 'b 'c 'd 'e 'f)
> (my-union '(a b c) '(b c))
(list 'a 'b 'c)
Some Set Operations: Union


Page 50
50
99
(my-merge '(1 2 6 8 1) '(3 4 7 9))
(1 2 3 4 6 7 8 1 9)
(my-merge '(1 2 3 6) '(3 4 5 7))
(1 2 3 3 4 5 6 7)
(my-merge '(1 3 4) '())
(1 3 4)
Execute Merging Two Lists
100
Merging Two Lists
(defun my-merge (los1 los2)
(if (null los1)
los2
(if (null los2)
los1
(if (< (car los1) (car los2))
(cons (car los1)
(my-merge (cdr los1) los2))
(cons (car los2)
(my-merge los1 (cdr los2)))))))


Page 51
51
101
Higher-Order Functions
Higher-order functions or functionals are functions which
do at least one of the following:
Take one or more functions as an input.
Produce as output a function.
The derivative in calculus is a common example, since it
maps a function to another function.
102
Higher-Order Functions
The functions
f: X
Y
and
g: Y
Z
can be composed by
first applying
f
to an argument
x
and then applying
g
to
the result.
Thus one obtains a function
g o f: X
Z
defined by
(g o f)(x) = g(f(x))
for all
x
in
X
.
The notation
g o f
is read as “g circle f” or “g composed
with f”.


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:
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

Students also viewed documents