[Haskell-cafe] stuck at the first part of chapter 1 of the CIS course

Brendan Hay brendan.g.hay at gmail.com
Thu Feb 5 08:39:54 UTC 2015


Hi Roelof,

The parse error is due to the colon ':' in the guards, where instead an
equals '=' should be used:

toDigits :: Integer -> [Integer]
toDigits n
  | n < 0 = []
  | otherwise = n/10 : []

You can see some more examples of the guard syntax under the heading
'Guards, guards!' in LYAH:
http://learnyouahaskell.com/syntax-in-functions#guards-guards

Hope that helps,
Brendan

On 5 February 2015 at 08:48, Roelof Wobben <r.wobben at home.nl> wrote:

>  Hello,
>
> I have to do this :
>
> Exercise 1 We need to first find the digits of a number. Define the
> functions
> toDigits :: Integer -> [Integer]
> toDigitsRev :: Integer -> [Integer]
> toDigits should convert positive Integers to a list of digits. (For 0 or
> negative inputs, toDigits should return the empty list.) toDigitsRev
> should do the same, but with the digits reversed.
> Example: toDigits 1234 == [1,2,3,4]
> Example: toDigitsRev 1234 == [4,3,2,1]
> Example: toDigits 0 == []
> Example: toDigits (-17) == []
>
>
> I have this so far :
>
> -- | convert a number to a array in pieces where a negative number will be
> a empty array.
> toDigits :: Integer -> [Integer]
> toDigits n
>   | n < 0 : []
>   | otherwise : n/10 : []
>
> -- | Main entry point to the application.
> module Main where
>
> -- | The main entry point.
> main :: IO ()
> main = do
>     toDigits 123
>
> but now I see this error message :
>
> src/Main.hs at 5:3-5:4
> parse error on input |
>
> This is not homework because I do this as a self-study.
>
> Roelof
>
>
>
>
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150205/8b35f2c9/attachment.html>


More information about the Haskell-Cafe mailing list