Both the following declarations work for replicate' 3 5
. I am not able to understand why you need Num and Ord when Integral would do. The second one was what I came up with and the first one was here - http://learnyouahaskell.com/recursion.
What do I lost with just Integral?
1
replicate' :: (Num a, Ord a) => a -> b -> [b]
replicate' 0 x = []
replicate' n x = x:replicate' (n-1) x
2
replicate' :: (Integral a) => a -> b -> [b]
replicate' 0 x = []
replicate' n x = x:replicate' (n-1) x
Note: I need to give some clarification here after willem's answer below (he understood the question without this clarification). The code in http://learnyouahaskell.com/recursion is
replicate' :: (Num i, Ord i) => i -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x:replicate' (n-1) x
Not what I have mentioned in 1. Willem's reply explains all three snippets.
Also, a related question is available at Haskell type definition, => etc
Ord
andNum
exist in the first place? Or are you asking why LearnYouAHaskell teaches the first one instead of the second? – Frank SchmittIntegral
would be better.replicate' 3.3 5
shouldn’t do what it does. – Ry-Integral
impliesNum
andOrd
. – Willem Van Onsem