stuck at the first part of chapter 1 of the CIS course

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
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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

'module Main where' has to go at the top of the file. You could also omit it. I would suggest you direct these kinds of questions to the haskell-beginners mailing list https://www.haskell.org/mailman/listinfo/beginners or the #haskell-beginners IRC channel on Freenode. Tom

On 6/02/2015, at 12:19 am, Roelof Wobben
it's now looking like this :
-- | 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 : []
I've seen you thinking the same way in the Erlang mailing list. Since this can only return a list of one element, it *must* be wrong. Also, n / 10 cannot be an integer, but the result is supposed to be a list of integers. You need `div` and `mod`, not / . You need to think recursively. Q How can toDigits n ==> [] arise? A When n is less than or equal to zero. Q How can toDigits n ==> [d1,d2,...,dm] arise? A When n is greater than zero, dm is n `mod` 10, and [d1,d2,...] without dm at the end is toDigits (n `div` 10). toDigits n | n <= 0 = [] toDigits n | n > 0 = toDigits (n `div` 10) ++ [n `mod` 10] There is another solution that builds up the answer from right to left using a helper function. In this case, I based an induction on the output I wanted to generate. Another way to think about it is to *pretend* that the integers are defined by a data type like --data Integer -- = Positive Integer Digit {- div 10, mod 10 -} -- | NonPositive (note the comment signs; this is NOT how integers are defined, but by using x > 0 -vs- x <= 0 we can tell which case applies and by using x `div` 10 and x `mod` 10 we can extract the "components" of an integer. We are PRETENDING that integers are a back-to-front list.) This (pretended) data type is just like a list except that it branches left 1234 = Positive (Positive (Positive (Positive NonPositive 1) 2) 3) 4) instead of right. Given this view of integers, we can process them using code that is structurally very much like the recursive code we would use to process lists. Just as we would write f (Positive higher least) = g (f higher) least f (NonPositive) = e so we can write f n | n > 0 = g (f (n `div` 10)) (n `mod` 10) f n | n <= 0 = e GIVEN a schema like that, you just have to ask yourself "what should e be? what should g be?"
-- | Main entry point to the application. module Main where
-- | The main entry point. main :: IO () main = do toDigits 123
This makes has to be wrong because toDigits returns a list of integers, but a list of integers is not an IO action, which is what main needs. To put it another way, there is nothing in the expression (toDigits 123) that causes any output to occur. You probably want something like main = print (toDigits 1234)
participants (4)
-
Brendan Hay
-
Richard A. O'Keefe
-
Roelof Wobben
-
Tom Ellis