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 gcd already exists.
The second one is also simple. In Haskell, main is the entry point of the program. Since the program needs some way to do IO, main has to have a type in the form IO a, usually IO (). What this means is you should call your gcd function something else. (The main function in Haskell is akin to the main method in Java.)
The usual practice is to call it gcd', which is pronounced "gcd prime". In this case, naming your function gcd' signifies that it is just a different implementation of gcd.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With