Solution of LISP Assignment 2011

# Solution of LISP Assignment 2011 - (defun plus-map(L1...

This preview shows pages 1–3. Sign up to view the full content.

;; Solution of LISP Programming Assignment 5/7/2011 ;;A function that takes a list L and an item X and returns L after the ;;insertion of the item X at the end of L. (defun insert (X L) (append L (list X) )) ;;A function that takes an item X (to be inserted) and an item Y ;;(used as a position) and a list L; and returns L with X inserted after the first ;;occurrence of the item Y. If Y is not in L, then it returns L. (defun insert-after(x y L) (cond ((null L) nil) ((equal (car L) y) (cons y (cons x (cdr L)))) (t (cons (car L) (insert-after x y (cdr L)))) ) ) ;;A function that takes a list L and an item X and returns L after ;;the removal of the first occurrence of item X (defun my-remove(x L) (cond ((null L) nil) ((equal (car L) x) (cdr L)) (t (cons (car L) (my-remove x (cdr L)))) ) )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
;;a function that adds the corresponding elements of two lists L1 and ;;L2 and returns a list of the results.
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (defun plus-map (L1 L2) (mapcar '+ L1 L2) ) ;;a function that counts the number of atoms in a list L regardless of ;;their nesting level (defun my-count(L) (cond ((null L) 0) ((atom (car L)) (+ 1 (my-count (cdr L)))) (t (+ (my-count (car L)) (my-count (cdr L)))) ) ) ;;a function that takes as input a predicate P and a list L then returns a list of ;;those elements of L that satisfy the predicate P. (defun filter(P L) (cond ((null L) nil) ((funcall P (car L)) (cons (car L) (filter P (cdr L)))) (t (filter P (cdr L))) ) ) ;;a function that return the reverse of its input list L. (defun my-reverse(L) (cond ((null L) nil) ((null (cdr L)) L) (t (append (my-reverse (cdr L)) (list (car L)))) ) ) ;Exercise 10-25: Define a functional that returns the composition of two functions (comp f g). That is, ((comp f g) x) = (f (g x)) (defun comp(f g) (function (lambda (x) (funcall f (funcall g x)))) )...
View Full Document

{[ snackBarMessage ]}

### Page1 / 3

Solution of LISP Assignment 2011 - (defun plus-map(L1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online