Haskell type errors?

Viewed 808

I'm really new to Haskell, and it's giving me quite a time. I was trying to write a basic function similar to 'linspace' in Matlab but the compiler seems to reject the idea that 'floor' produces an Integral type. My code:

linspace :: Double -> Double -> Double -> [Double]
linspace x dx y
    | y' == y   = [x + i * dx | i <- nums]
    | otherwise = ([x + i * dx | i <- nums] ++ [y])
    where
        n = floor ((y - x) / dx)
        nums = [0..n]
        y' = (x + (fromIntegral n) * dx)

Which produces an error in ghci:

maths.hs:8:21: error:
    * No instance for (Integral Double) arising from a use of `floor'
    * In the expression: floor ((y - x) / dx)
      In an equation for `n': n = floor ((y - x) / dx)
      In an equation for `linspace':
          linspace x dx y
            | y' == y = [x + i * dx | i <- nums]
            | otherwise = ([x + i * dx | i <- nums] ++ [y])
            where
                n = floor ((y - x) / dx)
                nums = [0 .. n]
                y' = (x + (fromIntegral n) * dx)

maths.hs:10:28: error:
    * No instance for (Integral Double)
        arising from a use of `fromIntegral'
    * In the first argument of `(*)', namely `(fromIntegral n)'
      In the second argument of `(+)', namely `(fromIntegral n) * dx'
      In the expression: (x + (fromIntegral n) * dx)
Failed, modules loaded: none.
2 Answers
Related