6
votes

So i'm very new to haskell and we've been set some coursework to create an algorithm using functional programming. So i've tried making a function that takes an array of integer and separates each digit into a list. However i keep getting the same error that the expected type isn't the same as the actual type and i don't understand what that means, even after doing some research on it. I've tried making a few functions that produce this error so could somebody please point out what exactly i am doing wrong and why this error keeps occurring in layman terms?

module Cswk2 where

getCard :: Integer -> [Integer]
getCard n
      | n < 0 = []
      | otherwise = lst_numb : getCard pre_numb
       where
         (pre_numb, lst_numb) = n divMod 10
1
looks like a very good attempt for a Haskell beginner - the only problem is that, as Willem points out in his answer, you forgot the backticks around divModRobin Zigmond

1 Answers

7
votes

The expression n divMod 10 makes no sense, since n is an Integer here, and thus you can not perform function application with n the function and divMod the parameter.

You can call the function with divMod n 10, so:

getCard :: Integer -> [Integer]
getCard n
    | n < 0 = []
    | otherwise = lst_numb : getCard pre_numb
    where (pre_numb, lst_numb) = divMod n 10

or you can use backticks to use divMod as an infix operator:

getCard :: Integer -> [Integer]
getCard n
    | n < 0 = []
    | otherwise = lst_numb : getCard pre_numb
    where (pre_numb, lst_numb) = n `divMod` 10