[Haskell-beginners] Got "Non type-variable argument in the constraint" error for a simple function
wizard
xie.zhiyi at gmail.com
Mon Feb 8 16:24:47 UTC 2016
Dear all,
I just started to learn Haskell with learnyouahaskell.com and at the very
beginning, I met a strange issue with following simple function:
-- why does work with "toZero 10" but not for "toZero -10"?toZero ::
(Integral t) => t -> [t]toZero 0 = [0]toZero x = if x > 0 then x :
toZero (x - 1)
else x : toZero (x + 1)
This function works as expected for positive arguments, e.g., "toZero 10"
gives me [10,9,8,7,6,5,4,3,2,1,0]. However, GHCI will raise following error
if I give it a negative argument, e.g., "toZero -10":
*Main> toZero -10
<interactive>:12:1:
Non type-variable argument in the constraint: Num (t -> [t])
(Use FlexibleContexts to permit this)
When checking that ‘it’ has the inferred type
it :: forall t. (Integral t, Num (t -> [t])) => t -> [t]
This seems strange to me as 10 and -10 has exactly the same type "Num a =>
a". I've done with chapter 1~10 of learnyouahaskell.com but still has no
idea on why this error. Anybody can help to explain this?
Thanks a lot.
Regards
Zhiyi Xie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160209/5f6c76d2/attachment.html>
More information about the Beginners
mailing list