https://www.theswamp.org/index.php?topic=38292
Thanks Lee, I've forgot about that thread.
I relooked at gile's examples of the sorting algorithms and indeed its really confusing (for me),
since at every (example) function theres a sub-defined recursive function and the main function definition also acts recursively!
Recursion was one of the hardest things to wrap my head around. I'm still in awe with some of the recursion I see here. ElpanovEvgeniy and MP are a couple that come to mind.
Hi Mark,
Writing a simple recursion felt fairly easy for me, since when iterating thru the list with
car and
cdr functions is not so mind-consuming.
And I realised that with recursion one could pretty much simulate any iterator in list (mapcar/vl-some/vl-every/while/repeat/vlax-for/...).
Even its possible to define functions that act similar to the standard ones (above listed), but to have different returns.
Or even define handy subfunctions that make the list manipulation even easier.
But yeah, the hardest thing to figure out is obviously
to perform sorting!
Few examples (
not related to sorting - just to see what I mean) :
; _$ (vl_every (lambda (x) (eq 'STR (type x))) '("A" 1 "B" 2 "C" "D" "E" 3 4 5 6 "F")) -> nil
; _$ (vl_every (lambda (x) (eq 'STR (type x))) '("A" "B" "C" "D" "E" "F")) -> (T T T T T T)
; _$ (vl_every numberp '(1 2 3 4 5 6 7 8 9 0)) -> (T T T T T T T T T T)
; _$ (vl_every (lambda (x) (if (numberp x) x)) '(1 4 5 1 2 6 3 7)) -> (1 4 5 1 2 6 3 7)
(defun vl_every
( f L
/ rec
) )
)
)
); defun vl_every
; _$ (_mapcar strcase '("a" "b" "c" "d" "e")) -> ("A" "B" "C" "D" "E")
; _$ (_mapcar (lambda (x) (strcat (strcase x) "1")) '("a" "b" "c" "d" "e")) -> ("A1" "B1" "C1" "D1" "E1")
(defun _mapcar
( f L
/ rec
) )
)
( (rec f L) )
)
); defun _mapcar
; _$ (vl_remove_if_not (lambda (x) (eq 'STR (type x))) '("A" 1 "B" 2 "C" "D" "E" 3 4 5 6 "F")) -> ("A" "B" "C" "D" "E" "F")
; _$ (vl_remove_if_not numberp '("A" 1 "B" 2 "C" "D" "E" 3 4 5 6 "F")) -> (1 2 3 4 5 6)
(defun vl_remove_if_not
( tf L
/ rec
) )
)
( (rec tf L) )
)
); defun vl_remove_if_not
; _$ (vl_positions (lambda (x) (eq 'STR (type x))) '("A" 1 "B" 2 "C" "D" "E" 3 4 5 6 "F")) -> (0 2 4 5 6 11)
; _$ (vl_positions numberp '("A" 1 "B" 2 "C" "D" "E" 3 4 5 6 "F")) -> (1 3 7 8 9 10)
(defun vl_positions
( tf L
/ rec
) )
)
( (rec tf L 0) )
)
); defun vl_positions
; _$ (GetNths '(0 2 5 8) (mapcar 'chr (vl-string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) -> ("A" "C" "F" "I")
; _$ (GetNths '(0 1 2 5 125) (mapcar 'chr (vl-string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) -> ("A" "B" "C" "F")
; _$ (GetNths '(0 2 5 8) (mapcar 'chr (vl-string->list "01234567890"))) -> ("0" "2" "5" "8")
; _$ (GetNths '(0 1 2 5 125) (mapcar 'chr (vl-string->list "01234567890"))) -> ("0" "1" "2" "5")
(defun GetNths
( nths L
/ rec
) )
)
( (rec nths L 0) )
)
); defun GetNths
Ofcourse some people say that recursion is really slow, but its not neccessary to use it everywhere (in some cases the user-delay is unnoticeable).
One way to learn is to translate existing solid code from another language to LISP.
For example, Visual BASIC (props to Randy Birch & VBNET) to LISP.
I thought about doing this, but I don't speak that many languages like you do!
And I suck at implementing algorithms... (time will tell about that).
Quick & Dirty ...
Appears to work:
(_QSort '(8 7 4 3 1 9 6 2 5)) >> (1 2 3 4 5 6 7 8 9)
Cheers.
Works perfect, and its retaining the duplicates like its supposed, I did a small comparsion from some of gile's examples:
_$ (_QSort '(8 0 7 11 4 3 1 15 9 0 6 11 14 2)) >> (0 0 1 2 3 4 6 7 8 9 11 11 14 15)
_$ (BubbleSort '(8 0 7 11 4 3 1 15 9 0 6 11 14 2)) >> (0 1 2 3 4 6 7 8 9 11 14 15)
_$ (InsertSort '(8 0 7 11 4 3 1 15 9 0 6 11 14 2)) >> (0 0 1 2 3 4 6 7 8 9 11 11 14 15)
The thing I really like is that your example is not so messed up:
-Define the required subfunctions for the task
-Execute
But I know thats your style of coding - very clean and readable. So thanks for that code! I'm gonna chew it for the next days.
BTW I'm a monkey mimic'ing such details like this - about maintaining the overall code (learing from you, learining from Lee Mac..). Revising as much as I can..