Home > Software design >  Haskell Error - Couldn't match type `[Int] -> String' with `[Char]'; Expected type
Haskell Error - Couldn't match type `[Int] -> String' with `[Char]'; Expected type

Time:10-21

I am trying to run the Hopscotch practice exercise in Haskell and I am getting this error when compiling it. Would appreciate any help.

I checked the parameters and the output and they seem correct.

THIS IS THE ERROR MESSAGE:

Histogram1.hs:11:16: error:
    * Couldn't match type `[Int] -> String' with `[Char]'
      Expected type: String
        Actual type: [Int] -> String
    * Probable cause: `(.)' is applied to too few arguments

THIS IS THE HASKELL CODE:

module Histogram where

import qualified Data.Map as M 

--- Type Aliases
type Count = Int 
type Counts = M.Map Int Count
type Indices = [Int]  

histogram :: [Int] -> String
histogram xs = histogram' . toCounts 
  where 
    histogram' :: Counts -> String 
    histogram' xs = 
      let (maxs, xs') = trim xs 
      in if null maxs 
            then footer 
            else asterisks maxs    histogram' xs' 
  
    footer :: String 
    footer = unlines ["=========="
                      , "0123456789"
                      ]  


toCounts :: [Int] -> Counts 
toCounts = foldr insertOrAdjust M.empty 

asterisks :: [Int] -> String 
asterisks ns = map (\n -> if n `elem` ns then '*' else ' ') [0..9]    "\n"

CodePudding user response:

The issue is in this line

histogram xs = histogram' . toCounts

it should either be

histogram xs = histogram' $ toCounts xs

or

histogram = histogram' . toCounts

What you wrote was function composition which yields [Int] -> String but then you have declared another variable.

  • Related