I need to define a haskell function:
func :: Int -> Int func 1 = 1 func 2 = 2 func x = x+1 So that it allows only positive numbers. I've already had a look at a similar question: Non-negative integers
And wrote this:
newtype Positive a = Positive a toPositive :: (Num a, Ord a) => a -> Positive a toPositive x | x < 0 = error "number cannot be negative" | otherwise = Positive x func :: Positive a -> a func (Positive n) = n Which is however already throwing errors. Thoughts?
Update:
Sample error:
*Main> func 1 <interactive>:32:6: No instance for (Num (Positive a0)) arising from the literal `1' Possible fix: add an instance declaration for (Num (Positive a0)) In the first argument of `func', namely `1' In the expression: func 1 In an equation for `it': it = func 1 *Main>
func $ toPositive 1Positive a0' with actual typea1 -> Positive a1' In the first argument of(-)', namelytoPositive' In the second argument of($)', namelytoPositive - 1' In the expression: func $ toPositive - 1