Sunday 1 May 2016

Haskell: undefined


Haskell Prelude.hs file defined undefined like below.

undefined        :: a
undefined        =  error "Prelude.undefined"

For example,

Sample.hs
factorial :: Integer -> Integer
factorial num
    | (num < 0) = undefined
    | (num == 0) = 1
    | (num > 0) = num * factorial(num-1)

Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> factorial 5
120
*Main> 
*Main> factorial (-10)
*** Exception: Prelude.undefined

Previous                                                 Next                                                 Home

No comments:

Post a Comment