Reputation: 19
For the purposes of the example, let's assume that everything is an integer. Also, I am aware that the compiler will be mad that the functions don't account for every case.
Let's say I have these two functions:
let f1 = function
| a -> b;;
let f2 = function
| c -> d;;
Is there any way I can make a function that does this:
| a -> b;;
| c -> d;;
by putting together f1 and f2 (in a style similar to concatenating 2 lists) without explicitly hardcoding them together?
Upvotes: 1
Views: 407
Reputation: 1895
let concat_funs f1 f2 x =
try f1 x
with Match_failure _ -> f2 x
and it is associative, but not commutative.
Upvotes: 2
Reputation: 66818
For homework, I think the rule is that you should show some code you tried and say what seems to be going wrong. Otherwise it's hard to comment without just solving the problem.
However, I think the fact that both f1
and f2
are partial might be a pretty big hint, assuming I understand what you're looking for (a higher-order function that can do this for any two functions similar to f1
and f2
).
Upvotes: 0