Re: Tail call optimization

2025-02-11 Thread Alexander Burger
On Mon, Feb 10, 2025 at 10:18:41PM -0800, Lindsay Lawrence wrote: > I realized several existing recursive functions I use were trivially > convertible and are now able to work with much larger lists. > > I put a few examples here: swap, mtf (move to front), mft (move from top) > > https://githu

How do I distinguish a list from cons pair?

2025-02-11 Thread Lindsay Lawrence
Is there a way to distinguish a list from a cons pair? The 'pair' function does not seem to do what I want. : (pair (cons 1 2)) -> (1 . 2) : (pair (list 1 2)) -> (1 2) : (pair (list 1 (2 3))) -> (1 (2 3)) : (pair (cons 1 (2 3))) -> (1 2 3) I would like to be able to distinguish the cons pair str

Re: How do I distinguish a list from cons pair?

2025-02-11 Thread GB
Hey, I'm curious myself honestly. Lists are kind of a virtual type, it's just a bunch of cons cells in a sequencial pattern. I think you'd need to write your own function that walks the list to see if its proper or improper (ends with non-nil). Best regards, Geri On Wed, Feb 12, 2025, 08:28 Li

Re: Tail call optimization

2025-02-11 Thread Lindsay Lawrence
> 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

Re: Tail call optimization

2025-02-11 Thread Alexander Burger
On Tue, Feb 11, 2025 at 02:41:01PM -0800, Lindsay Lawrence wrote: > > 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 >

Re: How do I distinguish a list from cons pair?

2025-02-11 Thread Alexander Burger
On Tue, Feb 11, 2025 at 10:16:14PM -0800, Lindsay Lawrence wrote: > Is there a way to distinguish a list from a cons pair? > > The 'pair' function does not seem to do what I want. It depends on how you define a list. I would say it is a cell with a non-NIL and non-atomic CDR, then (1 . 2) is not

Re: How do I distinguish a list from cons pair?

2025-02-11 Thread Alexander Burger
On Wed, Feb 12, 2025 at 08:31:03AM +0200, GB wrote: > Lists are kind of a virtual type, it's just a bunch of cons cells in a > sequencial pattern. Exactly. > I think you'd need to write your own function that walks the list to see if > its proper or improper (ends with non-nil). Right. 'fin' do