Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Couldn't match expected type `(a0 -> [a0]) -> [a0]', with
actual type `[a0]' (Roelof Wobben)
2. Re: Couldn't match expected type `(a0 -> [a0]) -> [a0]',
with actual type `[a0]' (Sumit Sahrawat, Maths & Computing, IIT (BHU))
----------------------------------------------------------------------
Message: 1
Date: Sat, 23 May 2015 08:58:50 +0200
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Couldn't match expected type `(a0 ->
[a0]) -> [a0]', with actual type `[a0]'
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
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
------------------------------
Message: 2
Date: Sat, 23 May 2015 17:29:05 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Couldn't match expected type `(a0 ->
[a0]) -> [a0]', with actual type `[a0]'
Message-ID:
<CAJbEW8MWma8_kT1RFX6hrWiSo+q9kW0qiCVhzYQw_qh=n67...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150523/cf0ab6e4/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 83, Issue 52
*****************************************