Saturday 4 June 2016

Haskell: Product of numbers from m to n

Suppose if m = 10, n = 17, then you should return 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17. If n < m, then return 0.


Sample.hs
productFromMToN :: Int -> Int -> Int
productFromMToN x y
    | (y < x ) = 0
    | (x == y) = x
    | otherwise =  x * productFromMToN (x+1) y

Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> productFromMToN 10 15
3603600
*Main> productFromMToN 0 5
0
*Main> productFromMToN 1 5
120
*Main> productFromMToN 5 1
0
*Main> productFromMToN 5 10
151200


Previous                                                 Next                                                 Home

No comments:

Post a Comment