Created
November 5, 2013 15:09
-
-
Save marsam/7320448 to your computer and use it in GitHub Desktop.
99 lisp problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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