Haskell 将参数转换为类型
Haskell Converting paramaters into type
我已经创建了 Contact 类型,我正在尝试创建一个函数,该函数采用 4 个参数(名字、姓氏、Phone 和州)并创建联系人并将其添加到列表中现有联系人。
type LastName = String
type FirstName = String
type Phone = String
type Contact = (Person, State)
data Person = Person Phone Name deriving (Show, Read)
type Name = (FirstName, LastName)
data State = Good | Bad
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s [] = Contact (Person c (p,n) ,s) : []
addContact c p n xs = Contact (Person c (p,n) , s) : xs
我似乎无法在 LYAH 或 SOF 上找到解决方案,我完全按照本节所说的进行操作,尤其是关于形状的部分:http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax 但我遇到以下编译错误:
• Data constructor not in scope:
Contact :: (Person, [Char]) -> Contact
我尝试将类型的大写改成小写,编译时仍然出现未定义变量错误
我在这里遗漏了什么吗?
Contact
不是类型构造函数,Contact
只是 (Person, State)
的别名,所以是 2 元组,因此 (,)
是数据构造函数:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = <b>(,)</b> (Person c (p,n)) s : xs
或更简洁:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = <b>(</b>Person c (p,n)<b>,</b> s<b>)</b> : xs
您 Person
的数据类型无效,它应该是:
data Person = <b>Person</b> Phone Name deriving (Show, Read)
或者您可以使用类型别名,例如:
type Person = <b>(</b>Phone<b>,</b> Name<b>)</b>
然后你可以这样实现:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = ((<b>Person</b> c (p, n)), s) : xs
或:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = (<b>(</b>c<b>,</b> (p, n)<b>)</b>,s) : xs
终于名字类型又出问题了,First
和Name
之间不能写空格,要用括号:
type Name = <b>(</b>FirstName<b>,</b> LastName<b>)</b>
我已经创建了 Contact 类型,我正在尝试创建一个函数,该函数采用 4 个参数(名字、姓氏、Phone 和州)并创建联系人并将其添加到列表中现有联系人。
type LastName = String
type FirstName = String
type Phone = String
type Contact = (Person, State)
data Person = Person Phone Name deriving (Show, Read)
type Name = (FirstName, LastName)
data State = Good | Bad
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s [] = Contact (Person c (p,n) ,s) : []
addContact c p n xs = Contact (Person c (p,n) , s) : xs
我似乎无法在 LYAH 或 SOF 上找到解决方案,我完全按照本节所说的进行操作,尤其是关于形状的部分:http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax 但我遇到以下编译错误:
• Data constructor not in scope:
Contact :: (Person, [Char]) -> Contact
我尝试将类型的大写改成小写,编译时仍然出现未定义变量错误
我在这里遗漏了什么吗?
Contact
不是类型构造函数,Contact
只是 (Person, State)
的别名,所以是 2 元组,因此 (,)
是数据构造函数:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = <b>(,)</b> (Person c (p,n)) s : xs
或更简洁:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = <b>(</b>Person c (p,n)<b>,</b> s<b>)</b> : xs
您 Person
的数据类型无效,它应该是:
data Person = <b>Person</b> Phone Name deriving (Show, Read)
或者您可以使用类型别名,例如:
type Person = <b>(</b>Phone<b>,</b> Name<b>)</b>
然后你可以这样实现:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = ((<b>Person</b> c (p, n)), s) : xs
或:
addContact :: Phone -> FirstName -> LastName -> State -> [Contact] -> [Contact]
addContact c p n s xs = (<b>(</b>c<b>,</b> (p, n)<b>)</b>,s) : xs
终于名字类型又出问题了,First
和Name
之间不能写空格,要用括号:
type Name = <b>(</b>FirstName<b>,</b> LastName<b>)</b>