Reputation: 261
Could anybody explain this output to me? Im completely new at Haskell and do not know why that happens.
import Data.Char
o=ord 'f'
main=do print (o==102)
print (mod (102^2087) 9797)
print (mod (o^2087) 9797)
Output:
xxx:~/Arbeitsfläche$ runhaskell st.hs
True
5253
0
GHC version 7.4.1, Ubuntu
Upvotes: 3
Views: 138
Reputation: 10037
Firstly, ord
is a function which returns the numerical representation of a character, in this case 102.
Your first line (print (o==102)
) is checking whether o
is equal to 102
which it is, hence the output True
.
The second line (print (mod (102^2087) 9797)
) applies two arguments to the mod
function. The mod
function returns the remainder when the first argument is divided by the second argument. This is integer division so no fractional parts are allowed. The caret operator (^
) means that the exponent is taken. i.e. "102 to the power of 2087".
The final line (print (mod (o^2087) 9797)
) does the same as the second line, with different arguments.
Hope that's clear!
Upvotes: 0
Reputation: 36011
This is because o
has type Int
which has a limited range and thus (mod (o^2087) 9797)
is also an Int
. However, the constant 102
is of a generic numeric type (Num a => a
) and (mod (102^2087) 9797)
is of generic integral type (Integral a => a
). When this generic integral type must be resolved to a concrete type, which happens when applying print
, the default resolution is to choose Integer
, an unbounded integral type. The details of this resolution are described in section 4.3.4 Ambiguous Types, and Defaults for Overloaded Numeric Operations of the Haskell 2010 Report.
Upvotes: 7