Friday, September 15, 2006

Reversing lists

There is more than one way to do a thing, and more than one language :)

Program 1
Language: Python
def reverse(l):
    if(l==[]):
        return []
    else:
        return (reverse(l[1:])+list(a[0]))


Program 2
Language: Python
def reverse(l):
    return l[::-1]

Program 3
Language: Python
a=range(10)
a.reverse()

Program 4
Language: elisp (!!)
(defun consx (l x)
  "like cons but first arg is a list"
  (if (eq l '())
      (list x)
    (cons (car l) (consx (cdr l) x))))

(defun reverse(l)
  "reverse a list"
  (if (eq l '())
      ()
    (consx (reverse (cdr l)) (car l))))

(reverse '(1 2 3))

2 comments:

Anonymous said...

u gEEk :P

Rohit said...

You seem to have too much time on your hands :P