Alex
Alex

Reputation: 667

basic haskell: error with simple function

I'm not sure what is wrong with my code, but when I try and run it I get

Couldn't match type `Integer' with `Int'

I'm using GHCi. I want to create a basic program that will go through the shop and give me all the customer names so I can then do a search to find out what item they have rented (a library). Is there a better way of getting the names?

This is my code:

type Name = String
type Customer = (Name,Int)
type shop = [Customer]
shop = [cust1, cust2]

cust1 = ("Neil", 311)
cust2 = ("Fred", 0)

getName :: (String,Int) -> Name
getName (a,b) = a 

Upvotes: 2

Views: 67

Answers (1)

bheklilr
bheklilr

Reputation: 54078

GHCi will default to using Integer over Int. You should specify the type of your tuples as cust1 = ("Neil", 311 :: Int) or cust2 = ("Fred", 0) :: (String, Int).

Edit after updates

If you already have Customer defined, you should write it as

cust1 = ("Neil", 311) :: Customer
cust2 = ("Fred", 0) :: Customer

getName :: Customer -> Name
getName (a, b) = a

You could also simplify things a bit by defining getName as

getName :: Customer -> Name
getName = fst

using ETA reduction and the built-in function fst

Upvotes: 4

Related Questions