简体   繁体   English

Haskell:将元素添加到数据类型内的列表

[英]Haskell: Adding an element to a list inside a data type

I'm trying to implement a function that adds an element (a type) to a list of this type to an existing created datatype.我正在尝试实现一个 function ,它将一个元素(一种类型)添加到该类型的列表中,并添加到现有创建的数据类型中。 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.由于 Haskell 数据变量是不可变的,我知道你必须创建一个与第一个具有相同特征的新数据并添加到它,但我似乎可以让 ghci 编译我的程序并让它做我想做的事情完成。

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:这是我的代码和 function 应该将联系人添加到我一直在尝试处理的现有联系人addContact的列表中:

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.我希望能够将另一个联系人添加到 owner1 的联系人列表中。

What is the correct way to go about this. go 关于这个的正确方法是什么。 SO and LYAH solution proposed around these type of case don't seem to help.围绕此类案例提出的 SO 和 LYAH 解决方案似乎无济于事。

There are multiple issues in your code.您的代码中有多个问题。 The fixed version of the addContact function: 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. Contact不是构造函数,它是类型别名。
  • fa(b) is the same as fab . fa(b)fab相同。 So Owner phone(x) is the same Owner phone x , the same applies to the other arguments.所以Owner phone(x)是相同的Owner phone x ,同样适用于其他 arguments。
  • Take a look at the signature of (++):: [a] -> [a] -> [a] .看一下(++):: [a] -> [a] -> [a]的签名。 You are trying, contacts(ow) ++ (Contact ((f, l), (e, n)) .您正在尝试, contacts(ow) ++ (Contact ((f, l), (e, n))
  • fab ++ [1,2,3] is the same as (fab) ++ [1,2,3] . fab ++ [1,2,3](fab) ++ [1,2,3]相同。 What you wanted is fa (b ++ [1,2,3]) or fa $ b ++ [1,2,3] .你想要的是fa (b ++ [1,2,3])fa $ b ++ [1,2,3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM