Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Johann-Tobias Schäg
> > > It is quite inefficient, as it needs to build a new list of > all 
> > > arguments on
> > > each call:
> > Well i see ways how it could be speed up it up in assembly but only by 
> > adding
> > lots of complexity.

> Yes. I'm afraid it needs very much complexity!


> Interestingly, 'foldr' would probably be extremely
> simple and efficient compared
> to that.

Here is an idea: implement foldl by reverse and foldr

> but I *feel* it must
> be something along the line:

   (de foldr (F I . @)
  (pass mapc '(@ (setq I (pass F I )

This gives us a new was to make large numbers:

(foldr '** 2 (range 1 10))
# wanted to post it but converting to decimal took way to long.

> Am I right?
you are incredible! I do not even understand how it works fully ...

> ☺/ A!ex

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Alexander Burger
On Tue, Oct 09, 2018 at 09:57:48PM +0200, Johann-Tobias Schäg wrote:
> > It is quite inefficient, as it needs to build a new list of > all arguments 
> > on
> > each call:
> Well i see ways how it could be speed up it up in assembly but only by adding
> lots of complexity.

Yes. I'm afraid it needs very much complexity!


Interestingly, 'foldr' would probably be extremely simple and efficient compared
to that.

I have not tested, and not even studied the specs deeply enough (and too lazy at
the moment to dig out the link in Jon's original mail ;), but I *feel* it must
be something along the line:

   (de foldr (F I . @)
  (pass mapc '(@ (setq I (pass F I )

Am I right?
☺/ A!ex

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread John Duncan
Yes, it makes more sense in reducing type languages like scheme and
Haskell, where its operating model is identical to the recursion that would
normally be implemented.

On Tue, Oct 9, 2018, 4:03 PM Johann-Tobias Schäg 
wrote:

>
> > It is quite inefficient, as it needs to build a new list of > all
> arguments on
> > each call:
> Well i see ways how it could be speed up it up in assembly but only by
> adding lots of complexity.
>
>(conc (rest) (cons I))
>
> > and it has no advantage over a direct inline
> > expression of what it does IMHO.
> > Often it is contra-productive to over-abstract things.
>
> T
>
>


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Johann-Tobias Schäg

> It is quite inefficient, as it needs to build a new list of > all arguments on
> each call:
Well i see ways how it could be speed up it up in assembly but only by adding 
lots of complexity.

   (conc (rest) (cons I))

> and it has no advantage over a direct inline
> expression of what it does IMHO.
> Often it is contra-productive to over-abstract things.

T

PԔ � )mX�����zV�u�.n7�

Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Alexander Burger
On Tue, Oct 09, 2018 at 09:19:29PM +0200, Jakob Eriksson wrote:
> Add foldl to standard library?

I would not recommend that.

It is quite inefficient, as it needs to build a new list of all arguments on
each call:

   (conc (rest) (cons I))

and it has no advantage over a direct inline expression of what it does IMHO.
Often it is contra-productive to over-abstract things.

☺/ A!ex

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread John Duncan
You're right, I was wrong :)

John

On Tue, Oct 9, 2018 at 3:07 PM  wrote:

> On Tue, 09 Oct 2018 19:31 +0200, Alexander Burger wrote:
> > On Tue, Oct 09, 2018 at 01:21:07PM -0400, John Duncan wrote:
> > > Yes, you couldn't reuse the same initial value for a data structure
> like
> > > you would in scheme
> >
> > How do you mean that? As I said, this code is completely free of
> destructive
> > side effects. You can use and reuse any initial value.
>
> Indeed.
>
> $ pil
> (de foldl (F I . @)
>(pass
>   mapc
>   '(@
>  (setq I (apply F (conc (rest) (cons I ) ) )
> -> foldl
> : (setq I 0)
> -> 0
> : (foldl '+ 0 (1 2 3 4))
> -> 10
> : I
> -> 0
>
> "Inside" `foldl` there is some "Hulk smash!"ing going on, but not
> outside.  `foldl` owns `I` and smashes on it with impunity; that's not a
> problem.
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


-- 
John Duncan


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Jakob Eriksson
Add foldl to standard library?

> 9 okt. 2018 kl. 20:59 skrev r...@tamos.net:
> 
>> On Tue, 09 Oct 2018 19:31 +0200, Alexander Burger wrote:
>>> On Tue, Oct 09, 2018 at 01:21:07PM -0400, John Duncan wrote:
>>> Yes, you couldn't reuse the same initial value for a data structure like
>>> you would in scheme
>> 
>> How do you mean that? As I said, this code is completely free of destructive
>> side effects. You can use and reuse any initial value.
> 
> Indeed.
> 
> $ pil
> (de foldl (F I . @)
>   (pass
>  mapc
>  '(@
> (setq I (apply F (conc (rest) (cons I ) ) )
> -> foldl
> : (setq I 0)
> -> 0
> : (foldl '+ 0 (1 2 3 4))
> -> 10
> : I
> -> 0
> 
> "Inside" `foldl` there is some "Hulk smash!"ing going on, but not outside.  
> `foldl` owns `I` and smashes on it with impunity; that's not a problem.
> 
> -- 
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread rick
On Tue, 09 Oct 2018 19:31 +0200, Alexander Burger wrote:
> On Tue, Oct 09, 2018 at 01:21:07PM -0400, John Duncan wrote:
> > Yes, you couldn't reuse the same initial value for a data structure like
> > you would in scheme
> 
> How do you mean that? As I said, this code is completely free of destructive
> side effects. You can use and reuse any initial value.

Indeed.

$ pil
(de foldl (F I . @)
   (pass
  mapc
  '(@
 (setq I (apply F (conc (rest) (cons I ) ) )
-> foldl
: (setq I 0)
-> 0
: (foldl '+ 0 (1 2 3 4))
-> 10
: I
-> 0

"Inside" `foldl` there is some "Hulk smash!"ing going on, but not outside.  
`foldl` owns `I` and smashes on it with impunity; that's not a problem.

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread rick
On Tue, 09 Oct 2018 14:24 +, Mike wrote:
> My demo code to mimic racket's reference:
> https://bitbucket.org/mihailp/tankfeeder/src/default/foldl.l

Excellent!  I'm stealing this!  :)  Thanks!

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Alexander Burger
On Tue, Oct 09, 2018 at 01:21:07PM -0400, John Duncan wrote:
> Yes, you couldn't reuse the same initial value for a data structure like
> you would in scheme

How do you mean that? As I said, this code is completely free of destructive
side effects. You can use and reuse any initial value.

But you *can* call destructive operators in PicoLisp, and they are very powerful
sometimes. It depends on the use case.

☺/ A!ex

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread John Duncan
Yes, you couldn't reuse the same initial value for a data structure like
you would in scheme

On Tue, Oct 9, 2018, 12:15 PM Alexander Burger  wrote:

> > On Tue, Oct 9, 2018, 10:30 AM Mike  wrote:
> >
> > > hi all,
> > >
> > > My demo code to mimic racket's reference:
> > > https://bitbucket.org/mihailp/tankfeeder/src/default/foldl.l
>
> Perfect!
>
>
> On Tue, Oct 09, 2018 at 10:57:43AM -0400, John Duncan wrote:
> > Isn't it destructive to I?
>
> You mean the line (setq I (apply F (conc (rest) (cons I? Not in the
> sense
> "destructive" is used in the context of PicoLisp. Here only the value of
> 'I' is
> modified.
>
> The term "destructive" is used if cell structures are modified as a side
> effect.
> For example, 'conc' in general is destructive:
>
>: (setq A (1 2 3))
>-> (1 2 3)
>
>: (conc A (4 5 6))   # Concatenate lists
>-> (1 2 3 4 5 6)
>
>: A
>-> (1 2 3 4 5 6) # The list in 'A' is destructively modified
>
>
> However, the 'conc' in the above (conc (rest) (cons I)) is all right,
> because it
> modifies only the list returned by (rest), which is a "fresh" list (just
> created
> newly and not referred to by any other place).
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Alexander Burger
> On Tue, Oct 9, 2018, 10:30 AM Mike  wrote:
> 
> > hi all,
> >
> > My demo code to mimic racket's reference:
> > https://bitbucket.org/mihailp/tankfeeder/src/default/foldl.l

Perfect!


On Tue, Oct 09, 2018 at 10:57:43AM -0400, John Duncan wrote:
> Isn't it destructive to I?

You mean the line (setq I (apply F (conc (rest) (cons I? Not in the sense
"destructive" is used in the context of PicoLisp. Here only the value of 'I' is
modified.

The term "destructive" is used if cell structures are modified as a side effect.
For example, 'conc' in general is destructive:

   : (setq A (1 2 3))
   -> (1 2 3)

   : (conc A (4 5 6))   # Concatenate lists
   -> (1 2 3 4 5 6)

   : A
   -> (1 2 3 4 5 6) # The list in 'A' is destructively modified


However, the 'conc' in the above (conc (rest) (cons I)) is all right, because it
modifies only the list returned by (rest), which is a "fresh" list (just created
newly and not referred to by any other place).

☺/ A!ex

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread John Duncan
Isn't it destructive to I?

On Tue, Oct 9, 2018, 10:30 AM Mike  wrote:

> hi all,
>
> My demo code to mimic racket's reference:
> https://bitbucket.org/mihailp/tankfeeder/src/default/foldl.l
>
> (mike)
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subjectUnsubscribe
>


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Mike
hi all,

My demo code to mimic racket's reference:
https://bitbucket.org/mihailp/tankfeeder/src/default/foldl.l

(mike)

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


Re: Function similar to Scheme 'foldl'?

2018-10-09 Thread Jon Kleiser
Thanks for all the answers. The reason I asked was, I’ve been playing a bit 
with Racket doing some “heavy” floating-point math, and I got quite used to the 
‘foldl’ function. Then I couldn’t remember having seen that function or 
anything quite similar i PicoLisp.

/Jon

> On 9. Oct, 2018, at 04:51, r...@tamos.net wrote:
> 
> On Mon, 08 Oct 2018 19:15 +, Jon Kleiser wrote:
>> Hi,
>> 
>> Is there a PicoLisp function similar to the Scheme 'foldl'?
> 
> Hi Jon!
> 
> You've probably already gone there, but FWIW this is what I have used in the 
> past.
> 
> (de foldl (Op Init Xs)
>  (let (Acc Init)
>(for X Xs (setq Acc (Op Acc X)))
>Acc))
> 
> In action:
> 
> (de op (X Y) (list 'op X Y))  # The "recon" binary operator :)
> (test '(op 1 2) (op 1 2))
> 
> (test  # Normal usage
>  '(op (op (op (op (op 0 1) 2) 3) 4) 5)
>  (foldl op 0 (1 2 3 4 5)))
> 



Re: Function similar to Scheme 'foldl'?

2018-10-08 Thread John Duncan
Yeah, all foldl does in scheme or haskell is encapsulate this pattern. In
haskell and scheme they like it for being able to compose functions. For
example:

sum = foldl + 0

Perhaps not as useful in Picolisp, which is not exactly a functional
language in the same sense.

On Mon, Oct 8, 2018 at 5:13 PM Alexander Burger  wrote:

> On Mon, Oct 08, 2018 at 08:50:09PM +, Mike wrote:
> > >
> > > Is there a PicoLisp function similar to the Scheme 'foldl'?
> > ...
> > There is no foldl. This is too general and not required in core.
> > What you intent to do ?
> >
> > Truly, I cant imagine when somebody needs it, because
> >
> > (foldl cons '() '(1 2 3 4)) is (reverse)
> > and
> > (foldl + 0 '(1 2 3 4)) is (sum)
>
> Right. And for a general case, you can do
>
>(let Res 
>   (mapc '(@ (pass process-args 'Res))
>  Lst1
>  Lst2
>  Lst3 )
>   Res )
>
> ☺/ A!ex
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


-- 
John Duncan


Re: Function similar to Scheme 'foldl'?

2018-10-08 Thread Mike
> 
> Is there a PicoLisp function similar to the Scheme 'foldl'?
> 
> See description here:
> 
> https://docs.racket-lang.org/reference/pairs.html?q=foldl#%28def._%28%28lib._racket%2Fprivate%2Flist
> .rkt%29._foldl%29%29
> 

There is no foldl. This is too general and not required in core.
What you intent to do ? 

Truly, I cant imagine when somebody needs it, because

(foldl cons '() '(1 2 3 4)) is (reverse)
and
(foldl + 0 '(1 2 3 4)) is (sum)


(mike)

> ​/Jon

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