Riced Out Yugo
<--- Previous post                Random Post                Next post --->

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 ...my name. is. THE PLAGUE @ 2006-05-22 00:25:00
Direct link to post Write comment

<--- Previous post                Next post --->