Joy of Lisping

Not for fun, but actually started to have some fun with cutty parenthesis lurking around the code, has a more ‘machine’ like flavour coding with those symbols. But the serious fun has become a bit serious after grabbing the beauty of the Lisp. The beauty of Lisp is its limited types/keywords, with flexible extension for building maximal blocks. Here are some snippets written for fun but could be put under analysis. There exists lot of variants of Lisp for the snippets presented below is written in common lisp.

Reversing a List

;; Does a deep reversion. Reverses lists of lists recursively.
(defun revlist-recursive (list rest)
  (if (atom rest) list
    (revlist-recursive (cons (if (atom (car rest)) 
		       (car rest)
		     (revlist-recursive () (car rest))) list) (cdr rest))
    ))

;; Reverse without an append.
(defun revlist (list rest)
  (if (atom rest) list
    (revlist (cons (car rest) list) (cdr rest))
    ))

;; Simpler and straight forward.
(defun reverse (list)
  (if (atom list) list
    (append (reverse (rest list)) (first list))
    ))

Flattening a list

(defun flatten (x)
  (cond ((atom x) x)
	((atom (car x)) (cons (car x) (flatten (cdr x))))
	(t (append (flatten (car x)) (flatten (cdr x)))))
  )

Merging two lists into one (Variant of append)

(defun merge-dual (x y)
  (if (atom x)
      (cons x y)
    (let ((dummy nil))
      (setq x (revlist () x))
      (while (not (not x))
	(setq y (cons (car x) y))
	(setq x (cdr x))
	)) 
    y
    ))
 
joy_of_lisping.txt · Last modified: 2007/04/17 09:21 by ramasamy
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki