Reputation: 1675
i have written the follwing code:
data Genre = Nonfiction | Novel | Biography deriving (Eq, Show)
type Name = (String, String)
type Date = (Int, Int, Int)
type Book = ABook Genre
Name
String
Date
Int
deriving Show
genre :: Book -> Genre
genre (ABook g _ _ _ _) = g
author :: Book -> Name
author (ABook _ a _ _ _) = a
title :: Book -> String
title (ABook _ _ t _ _) = t
date :: Book -> Date
date (ABook _ _ _ d _) = d
pages :: Book -> Int
pages (ABook _ _ _ _ p) = p
year :: Book -> Int
year b = let (_ , _ , b) = date b in y
publishedIn :: Int -> [Book] -> [Book]
publishedIn x xs = filter findYear xs
findYear:: Book -> Bool
findYear x | year x == 2014 = True
| otherwise = False
The functions from genre until year are helpfunctions which I try to include in the higher order functions like filter etc. But the compiler shows me the following error message:
Parse error on input 'Show'
What can I do against that ?
Upvotes: 3
Views: 3094
Reputation: 48664
There are number of issues with your code:
1) type
is used for creating new name for an existing type. But in your case, this will result in compilation error because you are trying to define new data constructor here:
type Book = ABook Genre Name String Date Int deriving (Show)
So, use the data
keyword to create proper type and data constructor. i.e:
data Book = ABook Genre Name String Date Int deriving Show
2) There is a problem with your function year
:
year :: Book -> Int
year b = let (_ , _ , b) = date b in y
As you see, there is no y
there. Probably you meant something like this:
year b = let (_ , _ , y) = date b in y
With that being said, you should use the record syntax instead of manually creating helper function.
data Book = ABook {
genre :: Genre,
author :: Name,
title :: String,
date :: Date
}
Here, you get free accessor functions named genre
, author
, etc without writing them yourself.
Upvotes: 6
Reputation: 10783
type
defines a type synonym and what you want is a new data type. You can do this by replacing type Book = ...
with data Book = ...
. Also, there needs to be at least one space before deriving
since it is part of the data type definition.
Upvotes: 2