Arc Forumnew | comments | leaders | submitlogin
1 point by lojic 6171 days ago | link | parent

Thanks. Here's a version with afn after I got help on #arc:

  (def foldl (op initial sequence)
    (let iter (afn (result rest)
                (if (no rest)
                  result
                  (self (op result (car rest)) 
                        (cdr rest))))
      (iter initial sequence)))
but it uses self inside the lambda and iter outside. I'll look into rfn.


2 points by simonb 6171 days ago | link

I don't think rfn will help here. rfn is useful for macros to prevent shadowing self and perhaps some obscure tricks involving mutually recursive nested lambdas.

However in your case you can get rid of let with a simple function call:

  (def foldl (op initial sequence)
    ((afn (result rest)
        (if rest
          (self (op result (car rest)) (cdr rest))
          result))
     initial sequence))

-----

1 point by lojic 6171 days ago | link

Of course - I do that in JavaScript all the time. Thx for the tip. Of course, I still want to be able to define a lambda that is bound to a name that also calls itself by the name instead of using self within and the name outside.

-----

2 points by simonb 6171 days ago | link

I think that's one of the cases where CL labels form would be more elegant.

But luckily this is Lisp so it shouldn't be too hard to make with a wee bit smarter so it would transform

  (with (foo (fn ...)))
to

  (with (foo (rfn foo ...)))

-----