Reputation: 2629
I have a hard time understanding the Type of a certain SML function i need to create.
It is a helper function that should return the longest string from a list of strings.
The type should be: (int * int -> bool) -> string list -> string
How should i read this and how can i create a function that would match?
I started by following simple code:
fun helper(x,y)=x>y
But now i should have this method to return a string list and then a string. But i seem to be missing some points here.
Upvotes: 1
Views: 486
Reputation: 78579
This is the signature of a curried function: (int * int -> bool) -> string list -> string
(int * int -> bool)
: this is the first argument, and it is a function that receives a tuple of two integers and returns a boolean. Looks like a predicate function.string list
: this is the second argument, a list of stringsstring
: and this is the resulting type of your function.For instance, in the course of Programming Languages (which appears to be the source of the question) the function in question should look somewhat like:
fun longest_string_helper f xs = ...
Where
f
is a function value of type (int * int -> bool)
xs
is a list value of type string list
And of course the function returns a string
value.
Notice that the arguments in the function declaration are separated by spaces, and not within a tuple pattern. That evidences that this is a curried function. You can read about this in the course reading notes for section 3, under Another Closure Idiom: Currying and Partial Application (page 11).
Upvotes: 5