Reputation: 65
Whe I type out into terminal the code below, it can successfully display all the detail I need.
avg "Vivo" testDatabase
But when I try to show that it works with the code below, it gives me an error - It couldn't match type 'Float' with '[Char]'. How should I try to fix this error?
--the function for code
avg :: String -> [Spa] -> Float
avg avgAreaRating spas = fromIntegral (sum lvlrating) / fromIntegral (length lvlrating)
where area = spaArea avgAreaRating spas
lvlrating = concatMap getLevelRating area
--code
show 5 = putStrLn $ avg "Vivo" testDatabase
Upvotes: 2
Views: 91
Reputation: 18249
putStrLn needs a String
as its argument. Your error occurs because you are calling it on a Float
- if the types don't match, your code won't compile.
The reason it works when you just put avg "Vivo" testDatabase
into GHCi is that GHCi automatically and implicitly calls print on anything you pass to it. Unlike putStrLn
, print
can take as argument any type which can be converted to a string (via the Show
typeclass - which always includes Float
).
So all you need to do is replace putStrLn
with print
:
print $ avg "Vivo" testDatabase
Upvotes: 4