Download presentation

Presentation is loading. Please wait.

Published byMiracle Mellin Modified over 2 years ago

1
Lisp II

2
How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y) nil) ((equal (car x) (car y)) (equal (cdr x) (cdr y)))))

3
Some simple list processing examples (defun member (x l) (cond ((atom l) nil) ((equal x (car l)) (cdr l)) (T (member x (cdr l)))) (defun append (l1 l2) (if (null l1) l2 (cons (car l1) (append (cdr l1) l2))))

4
Variations on reverse ;; either of these does O(n^2) cons operations (defun reverse (l) (if (null l) nil (append (reverse (cdr l)) (list (car l))))) (defun reverse (l) (and l (append (reverse (cdr l)) (list (car l)))))

5
Variations on reverse ;; this tail recursive operation does O(n) conses (defun reverse (l) (reverse1 l nil) (defun reverse (l acc) (if (null l) acc (reverse (cdr l) (cons (car l) acc))))

6
Flatten I (defun flatten (l) (cond ((null l) nil) ; empty list do nothing ((atom (car l)) ; cons an atom onto flattend cdr (cons (car l) (flatten (cdr l)))) ; otherwise flatten head & tail and append results (t (append (flatten (car l)) (flatten (cdr l))))))

7
Flatten II ;; this version avoids append, which is expensive. (defun flatten (l) (flatten1 l nil)) (defun flatten1 (l acc) (cond ((null list) acc) ; all done ((atom l) ; stick atom on the front of acc (cons l acc)) ; of the accumulator (t (flatten1 (car l) (flatten1 (cdr l) acc)))

8
Higher order functions (defun mapcar (f l) (if (null l) nil (cons (apply f (list (car l))) (mapcar f (cdr l)))))

9
Mapcar II (defun mapcar1 (f l) (if (null l) nil (cons (apply f (list (car l))) (mapcar f (cdr l))))) (defun mapcar (f &rest args) "Apply f to successive cars of all ARGS. Return the list of results." ;; If no list is exhausted, (if (not (memq 'nil args)) ; apply function to CARs. (cons (apply f (mapcar1 'car args)) (mapcar* f (mapcar1 'cdr args)))))

10
The End

Similar presentations

OK

Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.

Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on book review writing Ppt on object oriented programming language Ppt on steve jobs download Ppt on historical monuments in delhi Ppt on brand marketing salary Ppt on gestational diabetes mellitus Ppt on area of equilateral triangle Ppt on central limit theorem sample Ppt on global warming and climate changes Ppt on physical layer of osi model