haskell - Conversion between Int and fractional values -
i programming in haskell , having trouble following code:
exactrootlist :: int -> int -> [int] exactrootlist ini end = [x | x<-[ini .. end], (floor (sqrt x)) == (ceiling (sqrt x))]
then, when execute:
> hugs myprogram.hs
i get
error instances of (floating int, realfrac int) required definition of exactrootlist
i not understand error.
my program should show list of numbers have exact root 4 or 9, on interval [a, b] , b 2 params of function. example:
exactrootlist 1 10
it must return
1 4 9
because between 1 , 10 1, 4 , 9 have exact root.
greetings!
if @ type of sqrt
see works on types instance of floating
:
> :t sqrt sqrt :: floating => ->
as know, int
not floating point value. need convert ints (the variable x
) using fromintegral
:
[x | x<-[ini .. end], let = fromintegral x in (floor (sqrt a)) == (ceiling (sqrt a))]
Comments
Post a Comment