Arc Forumnew | comments | leaders | submitlogin
Basic currying
7 points by twilightsentry 5397 days ago | 3 comments
Arc3's ac.scm has some support for currying, but it's not exposed. I put together some simple curry and reversed-curry combinators which seem fairly usable.

  (def on-rep (f x)
    (annotate (type x)
              (f (rep x))))
  
  (def papply (f args-key)
    (let wrap [fn args
                (apply _ (args-key args))]
      (case (type f)
        mac (on-rep wrap f)
        fn  (wrap f)
            (err "Can't curry:" f))))
  
  (defs
    >_ (f . xs) (papply f [+ xs _])
    _< (f . xs) (papply f [+ _ xs]))
Trivial example:

  (= dup (>_ n-of 2))


2 points by rntz 5397 days ago | link

I've never seen a convincing use case for currying macros; not sure why you bother with that. I find your use of 'args-key to enable reverse currying interesting - one wonders whether there are other possible uses. Personally I just use the following:

    (def curry (f . xs) (fn ys (apply f (join xs ys))))
    (def flip (f) (fn (x y . z) (apply f y x z))
As regards reversed currying and other more complex partial applications, I find the extended version of the [] syntax that I've cooked up for anarki's arc3.master branch to be quite nice - any symbol prefixed with an underscore is taken to be an argument to the function, and arguments are ordered alphabetically (with the special symbol '__ standing for the rest argument). So for example:

    arc> ([cons _2 _1] 'foo 'bar)
    (bar . foo)
    arc> ([list _ _a 0 __] 1 2 3 4)
    (1 2 0 (3 4))
In the first example, of course, [cons _2 _1] could be replaced with (flip cons).

-----

5 points by twilightsentry 5396 days ago | link

My motivation for currying macros came from the recent "afn with" discussion. I had a w/afn macro which looked like a manually-curried function, so I figured it wouldn't hurt to add it to the curry fn:

  (mac w/afn args
    `(w/rfn self ,@args))
vs.

  (= w/afn (>_ w/rfn 'self))
Your [] syntax definitely looks useful, but I use (fn ((x y))) more than (fn (x y)).

-----

3 points by rntz 5396 days ago | link

Huh. I hadn't thought of anaphoric macros (like afn and afnwith), but it's true: they're good examples of macro currying.

-----