Reputation: 13
I read this article.
This is a very simple example of a common pattern you will see throughout Haskell. Making basic functions that are obviously correct and then combining them into more complex functions. This way you also avoid repetition. What if some mathematicians figured out that 2 is actually 3 and you had to change your program? You could just redefine
doubleMe
to bex + x + x
and sincedoubleUs
callsdoubleMe
, it would automatically work in this strange new world where 2 is 3.
When I called doubleUs 3 4
it should return 21
because I redefined the doubleMe
function, but the function returned 14
.
Upvotes: 1
Views: 1142
Reputation: 370397
The article isn't talking about having two definitions of the same function in a program where the latter overrides the former. It's talking about going into your Haskell file and replacing the old definition with the new one.
Generally you can't define two functions with same name in the same scope at all. Your code compiled in GHCi because each definition you enter into GHCi is seen as its own let
statement, so it starts a new scope. So doubleUs
used the old definition of doubleMe
because the new one is in a different scope that doubleUs
doesn't have access to (and Haskell is lexically scoped, so definitions from inner scopes don't affect functions from the outer scope). If you entered that code into a Haskell file and tried to compile it with GHC, you'd get a warning about an unreachable case because both definitions of doubleMe
would be seen as a single definition with two patterns that overlap.
Upvotes: 6