-
Notifications
You must be signed in to change notification settings - Fork 5
2015 005 Addition of Fn module
Author: Andreas Rossberg
Last revised: August 16, 2015
Status: proposed
Discussion: issue #6
signature FN =
sig
val id : 'a -> 'a
val const : 'a -> 'b -> 'a
val apply : ('a -> 'b) * 'a -> 'b
val o : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c)
val curry : ('a * 'b -> 'c) -> ('a -> 'b -> 'c)
val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c)
val flip : ('a * 'b -> 'c) -> ('b * 'a -> 'c)
val repeat : int -> ('a -> 'a) -> ('a -> 'a)
end
structure Fn : FN-
val id : 'a -> 'a
is the identity function. Thus,id ais equivalent toa. -
val const : 'a -> 'b -> 'a
const ais a constant function that always returnsa. Thus,const a bis equivalent toa(except for possible side-effects inb). -
val apply : ('a -> 'b) * 'a -> 'b
apply (f,a)appliesftoa. Thus, it is equivalent tof a. -
val o : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c)
f o gis the function composition offandg. Thus,(f o g) ais equivalent tof (g a). This is the same as the globalooperator. -
val curry : ('a * 'b -> 'c) -> ('a -> 'b -> 'c)
curry ftransforms the binary functionfinto curried form. Thus,curry f a bis equivalent tof (a,b). -
val uncurry : ('a -> 'b -> 'c) -> ('a * 'b -> 'c)
uncurry ftransforms a curried functionfinto a binary function. Thus,uncurry f (a,b)is equivalent tof a b. This function is the inverse ofcurry. -
val flip : ('a * 'b -> 'c) -> ('b * 'a -> 'c)
flip fswitches the argument order of the binary functionf. Thus,flip f (a,b)is equivalent tof (b,a). -
val repeat : int -> ('a -> 'a) -> ('a -> 'a)
repeat n fis the n-fold composition off. Thus,repeat n f ais equivalent tof (... (f a)...), wherefoccursntimes.
Other combinators could be added. These are the once that I have needed most in practice.
Adopting this proposal should not affect existing programs.
The need for basic higher-order functions like id and const arises all the time in combination with other functionals. So far, the SML Basis has lacked any of these functions, although they are standard in the libraries of most other functional programming languages.
This module also provides a more natural home for the existing o operator.
- [2015-08-16] Proposed
