0

I'm trying to implement a function that adds an element (a type) to a list of this type to an existing created datatype. Since Haskell data variable are immutable, I know that you have to create a new data with the same characteristic as the first one and add onto it, but I can seem to make ghci compile my program and make it do what I'm trying to accomplish.

Here is my code and the function which should add a contact to a list of already existing contact addContact which I'm been trying to work on:

type Model = String
type Serie = String
type SerialNumber = String
type Type= (Model, Serie)
data Car =      Car
                SerialNumber
                Type
                deriving (Show, Read) 
type PastCars= (Car, ChangeCause)
data ChangeCause = Broken | Contract deriving (Show, Eq, Read)
type Phone = (Extension, Number)
type Number = String
type Extension = String
type Person = (FirstName, LastName)
type FirstName = String
type LastName = String
type Contact = (Person, Phone)

data Owner  = Owner Car Phone [PastCars] [Contact]
--Getters

listPastSN [] = []
listPastSN ((p,e):xs) = (serialNumber p, e):listPastSN xs

serialNumber :: Car -> String
serialNumber (Car s _)= s

car :: Owner -> Car 
car (Owner c _ _ _ ) = c

phone :: Owner -> Phone 
phone (Owner _ p _ _ ) = p

pastCar :: Owner -> [PastCars]
pastCar (Owner _ _ p _ ) = p

contacts :: Owner -> [Contact]
contacts (Owner _ _ _ c) = c

addContact :: FirstName -> LastName -> Extension -> Number -> Owner -> Owner
addContact f l e n ow = Owner car(ow) phone(ow) pastCar(ow) contacts(ow) ++ (Contact ((f, l), (e, n)))

Let's I have these data variable

car1 = Car  "X234X" ("Honda", "Civic")
car2 = Car  "X233X" ("Mazda", "3")
person1 =  Person "Peter" "Mcleod"
owner1 = Owner car1 ("888" , "4144144") [(car2, Broken)] [(person1,("123", "1231231")) ]

I want to be able to add another contact to the list of contact of owner1.

What is the correct way to go about this. SO and LYAH solution proposed around these type of case don't seem to help.

1 Answer 1

1

There are multiple issues in your code. The fixed version of the addContact function:

addContact f l e n ow = 
  Owner (car ow) (phone ow) (pastCar ow) $ (contacts ow) ++ [((f, l), (e, n))]
  • Contact is not a constructor, it's a type alias.
  • f a(b) is the same as f a b. So Owner phone(x) is the same Owner phone x, the same applies to the other arguments.
  • Take a look at the signature of (++) :: [a] -> [a] -> [a]. You are trying, contacts(ow) ++ (Contact ((f, l), (e, n)).
  • f a b ++ [1,2,3] is the same as (f a b) ++ [1,2,3]. What you wanted is f a (b ++ [1,2,3]) or f a $ b ++ [1,2,3].
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate the help and now the command has much less compilation error, but there is still one, would you have an idea what could cause this? error: • Couldn't match expected type ‘Person’ with actual type ‘(FirstName, LastName)’ • In the expression: (f, l) In the expression: ((f, l), (e, n)) In the second argument of ‘(++)’, namely ‘[((f, l), (e, n))]’
It means that the code at your end is different from the code at question. You changed the declaration of Person from a type alias into a data declaration and didn't update the answer I gave you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.