functional programming - Standard ML: Unsure of Type of Higher Order Function -
i'm reading harper's book, intro standard ml, , bit confused on section 11.3, returning functions.
1) defines function creates constant functions. writes:
"given value k, application constantly k
yields function yields k
whenever applied. here's definition of constantly
:
val = fn k => (fn => k)
the function has type 'a -> ('b -> 'a)
." makes sense me: supply value of type 'a , returns function returns value (of type 'a), regardless of input (of type 'b, may or may not same type 'a).
but says can define function as:
fun k = k
this looks function takes 2 arguments , returns first...but not function returns function...
what missing?
2) little later, harper discusses map function. understand basic map function. talks map function let apply passed-in function many lists (instead of calling our original map function several times same passed-in function each call). writes:
fun map' f nil = nil | map' f (h::t) = (f h) :: (map' f t)
"the function map defined has type ('a -> 'b) -> 'a list -> 'b list
. takes function of type 'a -> 'b argument , yields function of type 'a list -> 'b list
result."
i'm pretty lost here looks map' applies f of elements in list , returns resulting list. have thought of type:
('a -> 'b) * 'a list -> 'b list
where going wrong?
thanks help, bclayman
both of questions arise lack of clarity arguments function are. in sml, every function (recall functions values) takes precisely 1 argument. argument may tuple (of type 'a * 'b
still single argument (that needs destructured).
in sml, fun f x1 x2 ... = t
syntactic sugar val rec f = fn x1 => fn x2 => ... => t
. constantly k
evaluates fn => k
.
whether give map type ('a -> 'b) * 'a list -> 'b list
or ('a -> 'b) -> 'a list -> 'b list
library design issue, effect same. same thing, though first takes tuple of function , list, while second takes function first , returns function lists lists. called "currying" in programming languages literature. (the tuple version "uncurried", other "curried".)
Comments
Post a Comment