Arc Haskell
(f a b) f a b
[f a _] f a Haskell version returns a function because of implicit currying
((f a b) c) f a b c Applies (f a b) to c
In Haskell this works because of implicit currying, fixed-arity-functions and left-associative function application. There were already some currying functions and macros discussed in this forum.The "reverse currying" behaviour can also be achieved in Arc if the arity of a function is known. E.g. if f takes two arguments, (f a b c) can unambiguously be interpreted as ((f a b) c). This does not affect normal currying, so (f a) means [f a _]. ;implementation of currying
(def curry (arity fun)
(fn args
(if (is arity len.args)
(apply fun args)
(< len.args arity)
(curry (- arity len.args)
(fn args2 (apply fun (join args args2))))
(> len.args arity)
(apply (apply fun (firstn arity args))
(nthcdr arity args)))))
(zap [curry 2 _] curry)
;some examples
(= add (curry 2 +))
(= add2 (curry.2 map (add 2)))
(add2 (list 1 2 3))
;returns (3 4 5)
(map (curry.2 + 4) (list 1 2 3))
;returns (5 6 7)
;useful application of the concept:
;nested structure access withoud nested parantheses
(mac rc-obj args `(annotate 'rc-obj (obj ,@args)))
(defcall rc-obj args
(apply (curry.2 (fn (t k) t.k)) args))
(= tabl
(rc-obj tom (rc-obj age 28
eyecolor 'green
father (rc-obj name 'harry
age '54
eyecolor 'blue))
dick (rc-obj age 45
eyecolor 'brown)))
(tabl 'tom 'father 'age)
;returns 54
|