1

I have this list in which I want to go through and convert certain elements of the list into a string. The first 3 functions work, however, the last one (placeRainfull) does not. When I try to load the script, I get this error:

Couldn't match expected type ‘Place’ with actual type ‘[Place]’

I want the function to go through each element in the list and have it run through the addDayWithRainfull function.

Code

getRainfull :: Place -> (String, [Int])
getRainfull (Place p _ _ rf ) = (p, rf)

convrtIntArray :: [Int] -> [String]
convrtIntArray rainfullArray = map show [ i | i <- rainfullArray]

addDayWithRainfull :: (String, [Int]) -> String
addDayWithRainfull (p, rf) = p ++ " " ++ unwords (convrtIntArray rf)

placeRainful :: [Place] -> String
placeRainful places = addDayWithRainfull (getRainfull places)
4
  • How did you define Place? May 11, 2020 at 15:37
  • Please include the full error. There should be information about where exactly the error occurred.
    – A. R.
    May 11, 2020 at 15:38
  • 1
    getRainfull is defined for Place and not for [Place]. May 11, 2020 at 15:40
  • 1
    in placeRainful, getRainfull is applied to places which is a [Places] where getRainfull expects a Place... you shoud map
    – lsmor
    May 11, 2020 at 15:40

2 Answers 2

3

You've told the compiler that placeRainful takes a list of Places ([Place]), but then you're passing that list of places to getRainFull, which you have indicated takes only a single Place.

In other words, Haskell is inferring based on the implementation of placeRainful that it should have type Place -> String, but then you said that that's not it's type.

As a side note, based on this several similar questions you've asked, it seems like you're having trouble differentiating the types [Place] and Place. Haskell (and indeed, most programming languages) cares a great deal about this distinction. Think about the data that each type represent:

[1, 2, 3, 4, 5] :: [Int]

1 :: Int

These types need to be treated differently because it's very clear what a should be in this expression:

let a = 1 * 1

but it's not at all clear what it should be in this expression:

let a = [1, 2, 3] *  [1, 2, 3]
2

The problem is that you defined a function:

getRainfull :: Place -> (String, [Int])

which takes as input a single Place object, but in your placeRainful you write:

placeRainful :: [Place] -> String
placeRainful places = addDayWithRainfull (getRainfull places)

so here you expect to process a list of Places, that will not work. You can for example make use of concatMap :: Foldable f => (a -> [b]) -> f a -> [b] to process each Place in the list of places and then concatenate these together:

placeRainful :: [Place] -> String
placeRainful places = concatMap (addDayWithRainfull . getRainfull) places

or with [a] -> [[a]] -> [a]:

import Data.List(intercalate)

placeRainful :: [Place] -> String
placeRainful places = intercalate "," (map (addDayWithRainfull . getRainfull) places)

or perhaps you need to use some other function, but the general idea is that you will need something to pass elements of the list to getRainfull, not the entire list itself.

2
  • 1
    Are you able to show me an example of how to use the ``` intersperse ``` function instead of the concatMap May 11, 2020 at 15:50
  • @UnknownPerson: you likely want to use intercalate, otherwise it will return a list of strings. May 11, 2020 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.