Function type parameter values

I am teaching myself F #. My question comes from my attempts to solve Exercise 2.13 in Functional Programming with F #, which asks me to define the curry function as:

curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c

      

My current result

curry : f:('a * b' -> 'c) -> x:'a -> y:'b -> 'c

      

Question Parameter values? Is my solution correct, or does the correct solution have no shortcuts f :, x: and y :?

+3


source to share


2 answers


From a functional point of view (both internally and how it works, not as functional programming) what matters is whether it is equivalent. The way to test this is to check if the two are interchangeable.

Let's pretend you have defined your function like this (note that the type signature matches yours):

let curry f x y =
    f(x,y);;

val curry : f:('a * 'b -> 'c) -> x:'a -> y:'b -> 'c

      

Next, let's define a second function that takes a function of the required type signature as follows:

let makeCurried (f: ('a * 'b -> 'c)->'a->'b->'c) g : ('a -> 'b -> 'c) = f g;;

val makeCurried :
  f:(('a * 'b -> 'c) -> 'a -> 'b -> 'c) ->
    g:('a * 'b -> 'c) -> ('a -> 'b -> 'c)

      



Note that the first parameter makeCurried

takes a type function ('a * 'b -> 'c) -> 'a -> 'b -> 'c

.

Now test: can you pass a function curry

to makeCurried

for the first parameter? If so, then the two are equivalent.

makeCurried curry (fun (x,y)->x*y);;

val it : (int -> int -> int) = <fun:it@7>

      

Yes! Thus, the annotations on the type signature are irrelevant, since the type equivalents are equivalent.

+2


source


No, parameter labels are irrelevant to the solution for this exercise, so they were excluded from the expected solution. A function defined as let curry f x y = ...

and a function defined as let curry funcToCurry firstParam secondParam = ...

will be valid decisions if they do the right thing. In this exercise, what matters is the form of the function you get (that is, the types it takes and returns), not the names you give your parameters.



+3


source







All Articles