I'm learning F# and I'm trying the following exercise (exercise 4.10 of Functional Programming Using F#).
Declare an F# function
prefix
:'a list -> 'a list -> bool when a:equality
. The value of the expressionprefix[x_0;x_1;...;x_m] [y_0;y_1;...;y_n]
istrue
ifm<=n
andx_i=y_i
for0<=i<=m
, andfalse
otherwise.
I wanted to use pattern matching on the two inputs, but wasn't sure how to do this with a curried function (as opposed to a function taking a 2-tuple). So I wrote:
let rec prefix a b = (function
| ([], _) -> true
| (_, []) -> false
| (x0::xst,y0::yst) -> x0 = y0 && prefix xst yst) (a,b);;
I wasn't very happy with this, because I thought it would be better (clearer and more efficient) if the inner function called itself, so I tried declaring the inner function locally.
let prefix a b =
let rec prefixt = function
| ([], _) -> true
| (_, []) -> false
| (x0::xst,y0::yst) -> x0 = y0 && prefixt (xst,yst)
prefixt (a,b);;
Is this the right way to approach such a problem in F#?