Our professor asked us to think of a function in OCaml that has the type
'a -> 'b i.e. a function of one argument that could be anything, and that can return a different anything.
I thought of using raise in a function that ignores its argument:
let f x = raise Exit But the professor said there was a solution that doesn't require any function in the standard library. I'm confused: how can you make a 'b if you don't have one in the first place?
I'm asking here rather than on Stack Overflow because I want to understand what's going on, I don't want to just see a program with no explanation.
Asked By : Gilles
Answered By : rgrig
The skeleton is let f x = BODY. In BODY you must use x only in generic ways (for example, don't send it to a function that expects integers), and you must return a value of any other type. But how can the latter part be true? The only way to satisfy the statement "for all types 'b, the returned value is a value of type 'b" is to make sure the function does not return. There are exactly two possibilities: either BODY faults or it doesn't terminate. The function raise faults, the following doesn't terminate:
let rec f x = f x Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/302
0 comments:
Post a Comment
Let us know your responses and feedback