Couldn't match expected type `(a0 -> [a0]) -> [a0]', with actual type `[a0]'

Hello, For some reasons my file's are corrupted. I had repair them with success except this one. Here is the code : toDigits number | number <= 0 = [] | otherwise = toDigits' [] number where toDigits' acc 0 = acc toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod` 10)) main = print $ toDigits 123 and here is the error: Couldn't match expected type `(a0 -> [a0]) -> [a0]' with actual type `[a0]' The function `(number `mod` 10) : acc' is applied to one argument, but its type `[a0]' has none In the expression: ((number `mod` 10) : acc) (toDigits' (number `mod` 10)) In an equation for toDigits': toDigits' acc number = ((number `mod` 10) : acc) (toDigits' (number `mod` 10)) Roelof --- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com

Hi, The faulty expression is this one: ((number `mod` 10 ): acc) (toDigits' (number `mod` 10)) The first expression: ((number `mod` 10 ): acc) is a list. You concatenate a number to acc. Therefore what you are doing is calling a list with an argument: (toDigits' (number `mod` 10)). That's what the error says. The first expression is called with one argument, but its is a list ([a0]), which is not a function, therefore can not be called. I think you meant something like: toDigits' acc number = toDigits' ((number `mod` 10 ): acc) (number `div` 10) Best, Greg On 23.05.2015 08:58, Roelof Wobben wrote:
Hello,
For some reasons my file's are corrupted. I had repair them with success except this one.
Here is the code :
toDigits number | number <= 0 = [] | otherwise = toDigits' [] number where toDigits' acc 0 = acc toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod` 10))
main = print $ toDigits 123
and here is the error:
Couldn't match expected type `(a0 -> [a0]) -> [a0]' with actual type `[a0]' The function `(number `mod` 10) : acc' is applied to one argument, but its type `[a0]' has none In the expression: ((number `mod` 10) : acc) (toDigits' (number `mod` 10)) In an equation for toDigits': toDigits' acc number = ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
Roelof
--- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

The error lies here:
toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod`
10))
It should instead be
toDigits' acc number = (number `mod` 10) : (toDigits' acc (number `mod` 10))
My suggestion would be to look at it like a fold.
toDigits' :: Integral a => a -> a -> [a]
toDigits' acc 0 = [acc]
toDigits' acc n = n `mod` 10 : toDigits' acc (n `div` 10)
Now this gives the digits in the reverse order, so in toDigits, you can
reverse it.
A good exercise would now be to re-write this as a fold. Graham Hutton has
a good paper about it. [1]
The best way would be to directly convert the number to a string using
show, but that's not the point of the exercise.
[1]: https://www.cs.nott.ac.uk/~gmh/fold.pdf
On 23 May 2015 at 12:28, Roelof Wobben
Hello,
For some reasons my file's are corrupted. I had repair them with success except this one.
Here is the code :
toDigits number | number <= 0 = [] | otherwise = toDigits' [] number where toDigits' acc 0 = acc toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod` 10))
main = print $ toDigits 123
and here is the error:
Couldn't match expected type `(a0 -> [a0]) -> [a0]' with actual type `[a0]' The function `(number `mod` 10) : acc' is applied to one argument, but its type `[a0]' has none In the expression: ((number `mod` 10) : acc) (toDigits' (number `mod` 10)) In an equation for toDigits': toDigits' acc number = ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
Roelof
--- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat

Try this:
toDigits' :: Integral a => [a] -> a -> [a]
toDigits' acc 0 = acc
toDigits' acc n = toDigits' (n `mod` 10 : acc) (n `div` 10)
If the accumulator in a recursive function doesn't accumulate anything
during some recursive call, but is just passed on unchanged, then you
might as well make it a free variable. Or eliminate it entirely if it's
unused.
You can now use a functional programming idiom, and assign the function
toDigits to the function toDigits' [], like so:
toDigits :: Integral a => a -> [a]
toDigits = toDigits' []
On Sat, May 23, 2015 at 6:59 AM, Sumit Sahrawat, Maths & Computing, IIT
(BHU)
The error lies here:
toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod` 10))
It should instead be
toDigits' acc number = (number `mod` 10) : (toDigits' acc (number `mod` 10))
My suggestion would be to look at it like a fold.
toDigits' :: Integral a => a -> a -> [a] toDigits' acc 0 = [acc] toDigits' acc n = n `mod` 10 : toDigits' acc (n `div` 10)
Now this gives the digits in the reverse order, so in toDigits, you can reverse it.
A good exercise would now be to re-write this as a fold. Graham Hutton has a good paper about it. [1]
The best way would be to directly convert the number to a string using show, but that's not the point of the exercise.
[1]: https://www.cs.nott.ac.uk/~gmh/fold.pdf
On 23 May 2015 at 12:28, Roelof Wobben
wrote: Hello,
For some reasons my file's are corrupted. I had repair them with success except this one.
Here is the code :
toDigits number | number <= 0 = [] | otherwise = toDigits' [] number where toDigits' acc 0 = acc toDigits' acc number = ((number `mod` 10 ): acc) (toDigits' (number `mod` 10))
main = print $ toDigits 123
and here is the error:
Couldn't match expected type `(a0 -> [a0]) -> [a0]' with actual type `[a0]' The function `(number `mod` 10) : acc' is applied to one argument, but its type `[a0]' has none In the expression: ((number `mod` 10) : acc) (toDigits' (number `mod` 10)) In an equation for toDigits': toDigits' acc number = ((number `mod` 10) : acc) (toDigits' (number `mod` 10))
Roelof
--- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards
Sumit Sahrawat
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (4)
-
Grzegorz Milka
-
Mike Meyer
-
Roelof Wobben
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)