Part 2 Summer 2014 Web.pdf-Part II: Func...
Part_2_Summer_2014_Web.pdf-Part II: Functional Programming with LISP
Showing 36-40 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 36
36
71
Example 1
;returns absolute difference between
;two numbers
(
defun
absdiff (x y)
(
cond
((> x y) (- x y))
(
t
(- y x))))
72
Example 2: Tutorial
Write a function, which takes a list and an element, and
returns the original list with the first occurrence of the
element removed
.
(defun my-remove (lst elt)
(cond ((null lst) nil)
((equal (first lst) elt) (rest lst))
(t (cons (first lst)
(my-remove (rest lst) elt)))))


Page 37
37
73
Executing The Function
(my-remove '(a 1 c 2 c 7) 'c)
= (cons 'a (my-remove '(1 c 2 c 7) 'c))
= (cons 'a (cons '1 (my-remove '(c 2 c 7) 'c)))
= (cons 'a (cons '1 '(2 c 7)))
= (cons 'a '(1 2 c 7))
= '(a 1 2 c 7)
(my-remove '(a (1 q) 2) 'q)
= (cons 'a (my-remove '((1 q) 2) 'q))
= (cons 'a (cons '(1 q) (my-remove '(2) 'q)))
= (cons 'a (cons '(1 q)
(cons 2 (my-remove '() 'q))))
= (cons 'a (cons '(1 q) (cons 2 '())))
= (cons 'a (cons '(1 q) '(2)))
= (cons 'a '((1 q) 2))
= '(a (1 q) 2)
74
Recursive Functions
Every recursive function consists of:
One or more base cases, and
One or more recursive cases.
Each recursive case consists of:
Splitting the data into smaller pieces (for example, with
car
and
cdr
),
Handling the pieces, often with recursive calls, and
Combining the results into a single result.


Page 38
38
75
Some Guidelines for Writing
Functions
Unless the function is extremely simple, begin with a
cond
to break the logic into cases.
When handling lists, you would normally adopt a
recursive solution. Make sure you treat the
NULL
list as
a base case.
Normally you would operate on the
first
element
(
car
) and recur with the
rest
of the list (
cdr
).
76
To delete the first element, just recur on the
rest
.
To keep the first element as is, use
cons
to place it at
the head of the returning list (whose tail is determined by
the recursive call).
Use
else
(or
t
) to protect your logic from forgotten
cases.
Some Guidelines for Writing
Functions


Page 39
39
77
Example: Summing the Numbers in
a List
Base case: if (empty list) then sum is 0.
Inductive (or recursive) case: add the first element to the
sum of the rest of the elements.
(defun sum (lst)
(cond
((null lst) 0)
(t (+ (car lst) (sum (cdr lst))))))
78
We can unfold the definition of sum with an example:
(sum ‘(1 2 3)) is evaluated as
1 + sum(‘(2 3))
= 1 + 2 + sum(‘(3))
= 1 + 2 + 3 + sum(‘())
= 1 + 2 + 3 + 0
= 6
Example: Summing the Numbers in
a List


Page 40
40
79
Example: Multiplying the Numbers
in a List
This is very similar to the sum function.
(defun product (lst)
(cond
((null lst) 1)
(t (* (car lst) (product (cdr lst))))))
80
Example: Reversing a List by
Recursion
Base case: if (empty list) then reverse is also the empty
list.
Inductive (or recursive) case: concatenate the reversed
list (excluding the first element) to the first element.
(defun my-reverse (lst)
(cond
((null lst) ‘())
(t
(append (my-reverse (cdr lst))
(list (car lst))))))
(setf lst (list 'a 'b 'c 'd 'e 'f 'g))
> (my-reverse lst)
(list 'g 'f 'e 'd 'c 'b 'a)


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