Hi José,

> I've been thinking on the problem a bit and found another way to tackle
> it, using this incredibly *UGLY* function (all of them formatted as
> the output of 'pp, I have no idea how to make it more aesthetically
> pleasing):

Me neither ;-)

> (de lazy Prg
>    (let L (box (cons))
>       (con
>          (val L)
>          (list
>             (list
>                'cdar
>                (list
>                   'con
>                   L
>                   (list
>                      'list
>                      (list 'cons ''quote (cons 'prog Prg)) ) ) ) ) )
>       (val L) ) )
> ...
> As you can see, the resulting lambda modifies itself to become just the
> quoted result on the first call. 
> 
> The way 'lazy works is by first boxing a cons pair to form the head of
> the lambda expression. Then it concatenates a function body to that
> head that is itself a call to 'con to replace itself with the computed
> result in a quote. 


For a general-use 'lazy' functionn, I would use the one from the
RosettaCode task

   http://rosettacode.org/wiki/Formal_power_series#PicoLisp

It simply uses the 'cache' function which is intended for such purposes,
and has the advantage in that it lets you define "real" lazy functions
(i.e. with arguments):

   (de lazy Args
      (def (car Args)
         (list (cadr Args)
            (cons 'cache (lit (cons))
               (list 'pack (list 'char (list 'hash (caadr Args))) (caadr Args))
               (cddr Args) ) ) ) )

   (lazy foo ()
      (prinl "!")
      2 )

   : (foo)                                                                    
   !
   -> 2
   : (foo)
   -> 2


   : (lazy bar (X Y)
      (prinl "!!")
      (+ X Y) )
   -> bar

   : (bar 2 3)                            
   !!
   -> 5

   : (bar 3 4)
   !!
   -> 7

   : (bar 2 3)
   -> 5

   : (bar 3 4)
   -> 7

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

Reply via email to