Friday, July 18, 2008

Funny lisp code!

Hi!
Today I want to post a funny code... funny for me that I'm a lisp newbie.
Yesterday I'm starting learning lisp for pleasure.
Today in my strange brain jumps the idea to implement a mergesort.
I started the coding and all seems to be nice.
Since the last function where I take lots of strange behaviors.
The strange thing is that the program duplicate some numbers
{5,4,3,2,1} --a kind of-->{1,22,3333,4444,5}
Why?????
Let's change the method!
In the change I add some recursion to make this mergesort style

(setq M (round (/ I 2)))
(setq C (g_mergesort (g_firsthalf A M)))
(setq D (g_mergesort (g_secondhalf A M)))
(g_merge C D) )


but when I write the C assignment I launch a test with the previous mergesort system.
The code looks like this

(defun g_mergesort(A)
"The mergesort"
(setq I (list-length A))
;(print A)
(if (<= I 1) (return-from g_mergesort A))

(setq M (round (/ I 2)))
(setq C (g_mergesort (g_firsthalf A M))) ;blackbox

(g_merge (g_mergesort (g_firsthalf A M)) (g_mergesort (g_secondhalf A M))) )

It works o_O
but... in my mind not! I'm starting to write another way! this is a mix of the new and the previous code and it WORKS!!!!!
I looks to the recursion tree but scared I close it :D

This is the full code: LISP STRANGE MERGE SORT

enjoy!

No comments: