Skip to content

Instantly share code, notes, and snippets.

@ser1zw
Created June 29, 2013 22:39
Show Gist options
  • Save ser1zw/5892983 to your computer and use it in GitHub Desktop.
Save ser1zw/5892983 to your computer and use it in GitHub Desktop.
;; -*- mode: lisp-interaction -*-
;; http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
;; P01
(defun my-last (lst)
(if (null lst)
nil
(if (null (cdr lst))
lst
(my-last (cdr lst)))))
;; (my-last '(a b c d)) ;=> (d)
;; P02
(defun my-but-last (lst)
(if (null lst)
nil
(if (<= (length lst) 2)
lst
(my-but-last (cdr lst)))))
;; (my-but-last '(a b c d)) ;=> (c d)
;; P03
(defun my-element-at (lst idx)
(if (or (>= idx (length lst)) (> 0 idx))
nil
(if (= idx 1)
(car lst)
(my-element-at (cdr lst) (decf idx)))))
;; (my-element-at '(a b c d e) 3) ;=> c
;; P04
(defun my-length(lst)
(if (null lst)
0
(+ (my-length (cdr lst)) 1)))
;; (my-length '(a b c)) ;=> 3
;; P05
(defun my-reverse (lst &optional (rev-lst '()))
(if (null lst)
rev-lst
(my-reverse (cdr lst) (cons (car lst) rev-lst))))
;; (my-reverse '(a b c)) ;=> (c b a)
;; P06
(defun my-palindrome-p (lst)
(equal lst (reverse lst)))
;; (my-palindrome-p '(a b c b a)) ;=> t
;; (my-palindrome-p '(a b c b)) ;=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment