Reputation: 2179
something is wrong with my printf method . i am trying to print all the movies in the testDatabase. is the problem coming from the format characters i used in printf?
import Prelude
import Data.Char
import Data.Int
import Text.Printf
-- Types
type Title = String
type Actor = String
type Cast = [Actor]
type Year = Int
type Fan = String
type Fans = [Fan]
type Period = (Year, Year)
type Database = [Film]
testDatabase :: Database
testDatabase = [("The Gunman", ["Idris Elba", "Sean Penn", " Javier Bardem"], 2015,["Garry", "Dave", "Zoe", "Kevin", "Emma"]),
("The Shawshank Redemption", ["Tim Robbins", "Morgan Freeman", "Bob Gunton"],1994, ["Bill", "Jo", "Garry", "Kevin", "Olga", "Liz"]),
("The Dark Knight", ["Christian Bale", "Heath Ledger","Aaron Eckhart"], 2008, ["Zoe","Heidi", "Jo", "Emma", "Liz", "Sam", "Olga", "Kevin", "Tim"]),
("Inception", ["Leonardo DiCaprio", "Ellen Page"], 2010, ["Jo", "Emma", "Zack", "Olga", "Kevin"])]
printFilms :: Database -> IO()
printFilms [] = putStrLn "There are no films in the database"
printFilms filmList = sequence_ [printf("|%-s| |%-20s| |%2d| |%.1f|\n") title cast year fans | (title, cast, year, fans) <- filmList]
No instance for (IsChar [Char]) arising from a use of `printf'
Possible fix: add an instance declaration for (IsChar [Char])
In the expression:
printf ("|%s| |%-20s| |%2d| |%.1f|") title cast year fans
In the first argument of `sequence_', namely
`[printf ("|%s| |%-20s| |%2d| |%.1f|") title cast year fans |
(title, cast, year, fans) <- filmList]'
In the expression:
sequence_
[printf ("|%s| |%-20s| |%2d| |%.1f|") title cast year fans |
(title, cast, year, fans) <- filmList]
Upvotes: 2
Views: 182
Reputation: 18199
The problem is that cast
and fans
are lists of String
s, not String
s themselves. %s
only supports single strings. The error message is slightly confused by printf
's internal type magic, but essentially tells that it wants a Char
rather than a String = [Char]
.
You need to convert those lists to how you want them to get printed yourself. Something like
printFilms filmList = sequence_
[printf("|%-s| |%-20s| |%2d| |%.1f|\n") title (unwords cast) year (unwords fans)
| (title, cast, year, fans) <- filmList]
should allow you to get it to compile, although you probably want to replace unwords
(at least the first one) with your own subformatting functions.
Upvotes: 2