3 ms·
The type of the reduction function would just be something like (X, Y) -> (X Y) args pair The type of a reduce that has an initial seed is someth
by Veedrac 9y ago
The type of the reduction function would just be something like
(X, Y) -> (X Y)
args pair
The type of a reduce that has an initial seed is something like
X -> ((E, X) -> X) -> [E] -> X
seed reducer list result
The result is going to need to be some recursively defined type, which I'll call List,
type List E = E | (E (List E))
Apply these together trivially
(List E) -> ((E, (List E)) -> (List E))
-> [E]
-> (List E)
- BoiledCabbage 9y agoAnd this is the other thing I like about the haskell/ML collection of languages. The description you gave above is extremely concise and direct. If you're familiar with the language being used it's a very efficient form of communication. I've noticed that working in languages of that family gives you a vocabulary to talk about things that previously would have been very wordy to discuss. Languages with these types of type features provide easy abstractions to illuminate structure and patterns begin discussing new ideas that previously would've been considered on off code.
- Veedrac 9y agoFWIW, this can be inferred fully automatically. Type let pair = fun x -> fun y -> { _0 = x; _1 = y } let rec reduce = fun init -> fun combine -> fun xs -> match xs with [] -> init | x :: rest -> reduce (combine x init) combine rest let test = reduce 0 pair [1; 2; 3; 4] into https://www.cl.cam.ac.uk/~sd601/mlsub/ https://www.cl.cam.ac.uk/~sd601/mlsub/ to see a demonstration.