I am trying to load the following program
gcd a b = if b == 0
then a
else gcd b (rem a b)
But I get the error
Prelude> :l euclidean.hs
[1 of 1] Compiling Main ( euclidean.hs, interpreted )
euclidean.hs:3:8:
Ambiguous occurrence `gcd'
It could refer to either `Main.gcd', defined at euclidean.hs:1:0
or `Prelude.gcd', imported from Prelude
Failed, modules loaded: none.
I changed the function name from gcd to main and I get
Couldn't match expected type `IO t'
against inferred type `a -> a -> a'
In the expression: main
When checking the type of the function `main'
Failed, modules loaded: none.
I dont understand this. I am using the workshop step here.
The first error should be self-evident–a function called
gcdalready exists.The second one is also simple. In Haskell,
mainis the entry point of the program. Since the program needs some way to do IO,mainhas to have a type in the formIO a, usuallyIO (). What this means is you should call yourgcdfunction something else. (Themainfunction in Haskell is akin to themainmethod in Java.)The usual practice is to call it
gcd', which is pronounced “gcd prime”. In this case, naming your functiongcd'signifies that it is just a different implementation ofgcd.