Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't match expected type of Haskell function

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
like image 881
CadyOG Avatar asked May 19 '20 19:05

CadyOG


1 Answers

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
like image 147
Willem Van Onsem Avatar answered Sep 28 '22 00:09

Willem Van Onsem