> I usually approach such tasks with loops, with a rather
> different mental model.
>
>
That is interesting! Can you say more about your mental model?
I am often surprised by how often I end up with recursive solutions when
hacking on a particular problem in picolisp.

Concerning swap, we could for completeness mention that there is also
> the built-in 'xchg' function:
>
>    : (let L (1 2 3 4 5 6 7)
>       (xchg (nth L 3) (nth L 5))
>       L )
>    -> (1 2 5 4 3 6 7)
>

The swap function I implemented was an exercise after working with some
particularly large lists.

I did find after writing that code that there is xchg as well as a few
other useful functions in this vein in picolisp...'insert' and even 'swap'!
I sometimes don't realize (at first) when a particular function returns a
'pointer' into a list, vs a 'value'.
'nth' and 'put', in particular, are far more powerful than I initially
thought.

I wanted to do the swap action in a single pass  (and it was a good
exercise in understanding destructive list manipulation a bit better).

A slight change to your little function achieves the single pass
performance...in far less code than the function I shared!

: (let (L (1 2 3 4 5 6 7 8 9)
     lhs (nth L 3)
     rhs (nth lhs (inc (- 5 3))))
(xchg lhs rhs) L)
-> (1 2 5 4 3 6 7 8 9)


I have bottlenecked performance of my code in the past by not remembering
some functions have to necessarily traverse the list to do their work.
e.g. the recent json parser I implemented was calling 'length' at various
points in its process and had very poor performance over large sets until I
realized that.


Thanks Alex! As always, I appreciate the shared insight

/Lindsay

Reply via email to