May 22nd, 2006

  • roybot

CMU Common Lisp

(defun insert (x l) ; Simple enough. No recursivity needed.
  (append l (cons x 'nil))) ; hughlgaabgl
; (insert '4 '(1 2 3))
; should yield: (1 2 3 4)

(defun insert-after (X pos ls)
  (cond ( (null ls) ls)

        ( (eq pos (car ls))
           (append (cons X 'nil ) ls ))
           ;broke: (cons (car ls) (append (cons X 'nil ) (cdr ls) ))
        (  t  (cons (car ls)
                    (insert-after X pos (cdr ls))))))
; (insert-after '4 '5 '(1 2 3 5) )
; should yield: (1 2 3 4 5)

(defun my-remove (it ls)
  (cond ( (null ls) (warn "err: element doesn't exist"))
        ( (eq it (car ls)) ((cdr ls)))
        ( t (cons (car ls) (remove it (cdr ls))))))
; (remove '4 '(1 2 3 4 5))
; should yield: (1 2 3 5)

(defun plus-map (pri sec)
  (cond ( (and (null pri) (null sec))
          nil)
        ( (or (null pri) (null sec))
          (warn "one or the other list ran out"))
        ( t (cons (+ (car pri) (car sec)) (plus-map (cdr pri) (cdr sec))))))

;(plus-map '(1 2 3 4) '(1 9 8 4))
;should return (2 11 11 8).

;<jlf> mtp, you need to call count recursively on the car of its argument if the car is a list, and add either that
;         result or 1 if it was an atom to the result of calling count recursively on its cdr, if the cdr is non-nil

(defun my-count (ls)
  (cond ( (null ls) 0)
        ( (atom (car ls)) (+ 1 (my-count (cdr ls))))
        ( (listp (car ls)) (+ (my-count (car ls)) (my-count (cdr ls))))))
;(my-count '(a b (c 4) ((99)) nil t))
;should return 7

(defun filter (pred ls)
  (cond ( (null ls) nil)
        ( (funcall pred (car ls)) (cons (car ls) (filter pred (cdr ls)) ))
        ( t (filter pred (cdr ls)))))
;(filter 'minusp '(2 -3 7 -1 -6 4 8))
;should return (-3 -1 -6)
; also
;(filter 'listp '(a (b c) d (e f g)))
;should return ((b c) (e f g))

(defun my-reverse (ls)
  (cond ( (null ls) nil)
        ( t (append (last ls) (reverse (all-but-last ls))))))
(defun all-but-last (ls)
  (cond ( (null (cdr ls)) nil)
        ( t (cons (car ls) (all-but-last (cdr ls))))))
;(reverse '(1 2 3 4 5)
;should return (5 4 3 2 1)

(defun comp (f g)
  (function (lambda (x) (funcall f (funcall g x)))))

;How about?
;*(defun add1 (x) (+ x 1))
;
;*(defun mult2 (x) (* x 2))
;
;
;*(mapcar (comp 'add1 'mult2) '(1 2 3))
;
;    ? (3 5 7)


Posted by Mr. The Plague (mastar of teh gibson)
  • roybot

h4x3d

i d3cl4r3 w4r
0n n00b1d1Ty
w3'r3 dr0pP1ng b0tS w/ 4 c0mPut4T10n4L fLu1d1ty
4n 4tT4c|< w/ m34t 4nD ph1L0s0pHy
bU1Lt 0n 1337 4nD g3N3r0S1ty
w3'r3 t4k1n u & ur kR3w 0n 4 m1ss10n
we'r3 h4x1n m1nDs w/ c0mpL3T3 l4cK 0f pR3C1s10n
ur bR41n's m3sSed-uP, PWND!!1, 0uT 0f d4t3, sT0n3d
s0 r3l4x, k1c|< b4cK & 4nD t3LL t3h m4n h3's 0WNED!#$~

Posted by Reverend Tedward Q. Porktanker