Skip to content

Instantly share code, notes, and snippets.

@marsam
Created November 5, 2013 15:09
Show Gist options
  • Select an option

  • Save marsam/7320448 to your computer and use it in GitHub Desktop.

Select an option

Save marsam/7320448 to your computer and use it in GitHub Desktop.
99 lisp problems
;; 99 lisp problems
;; http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
;; P01
;; * (my-last '(a b c d))
;; (D)
(defun my-last (lst)
(cond
((null lst) nil)
((null (cdr lst)) (list (car lst)))
(t (my-last (cdr lst)))))
;; P02.
;; * (my-but-last '(a b c d))
;; (C D)
(defun my-but-last (lst)
(cond
((null lst) nil)
((<= (length lst) 2) lst)
(t (my-but-last (cdr lst)))))
;; P03.
;; * (element-at '(a b c d) 3)
;; C
(defun element-at (lst n)
(cond
((null lst) nil)
((= n 1) (car lst))
(t (element-at (cdr lst) (1- n)))))
;; P04
(defun number-elements (lst)
(if (null lst)
0
(1+ (number-elements (cdr lst)))))
;; P05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment