Your approach seems to be trying to use pattern matching like it's unification - Haskell doesn't perform unification, and pattern matching only works for things like data constructors (eg. Just
), not for values.
The name x
you're using in the inner definition shadows the name x
used in the outer definition - so your inner function is equivalent to:
f x = 10
You should use a different variable name, and make your inner function compare its argument to the outer function's argument:
g x = let f y = if x == y then 10 else 0
in f
Or using currying, which is arguably better style:
g x y = if x == y then 10 else 0
This is semantically equivalent to the above version, and is also semantically equivalent to a function returning a lambda. We can partially apply g
to a value to produce a function taking one parameter, eg:
> (g 3) 4
0
> (g "hi") "hi"
10