I have been attempting to learn PicoLisp by going through the
tutorials at http://www.prodevtips.com/tag/pico-lisp/. I'm finding it
rather difficult, though.

The third tutorial, "Working with tables in PicoLisp", has the following
code to sort a table:

(setq *People
      '((name John phone 123456 age 56)
        (name Fred phone 654321 age 35)
        (name Fred phone 236597 age 38)
        (name Hank phone 078965 age 23)))

(de assoc1d (Lst Key)
    (loop
     (NIL Lst NIL)
     (T (= Key (pop 'Lst)) (pop 'Lst))
     (pop 'Lst)))

(de getCol (Lst K)
    (mapcar '((Sub)(assoc1d Sub K)) Lst ) )

(println (getCol *People 'phone))

(de getSorting (Sorted Original)
    (make
     (while Original
            (let Value (pop 'Original)
               (setq Sorted
                  (place
                     (link (index Value Sorted)) Sorted NIL ) ) ) ) ) )

(de sort2d (L Key)
     (let Col (getCol L Key)
       (mapcar '((Pos)(get L Pos)) (getSorting (sort (copy Col)) Col) )))

(sort2d *People 'name)

This doesn't seem to work, though. PicoLisp responds with:

-> ((name Hank phone 78965 age 23) (name John phone 123456 age 56)
(name Fred phone 654321 age 35) (name Fred phone 236597 age 38))

It seems to work if I do this instead:

(de mysort2d (L Key)
   (make
     (let Col (getCol L Key)
        (let Sorted (getSorting (sort (copy Col)) Col)
           (for X (length L)
              (link (get L (index X Sorted))) ) ) ) ) )

-> ((name Fred phone 654321 age 35) (name Fred phone 236597 age 38)
(name Hank phone 78965 age 23) (name John phone 123456 age 56))

Still, it seems so complicated. Is there a simpler way of doing this?

Thanks, so much.

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to