列表中的Haskell列表



Hello有一个问题,如何在Haskell中创建这样的用户列表:

list = [("Frank",24,[wall, door],1),("Ann",45,[window],0),
("Claudia",44,[window, bed],2), ("Pedro"77,[wall],1)]

我试过这种方法,但不起作用:

type Home = (Homename)
type Homename = String
type Person = (Name, Age, [Home], Num)
type Name = String
type Age = Integer
type Num = Integer
list :: [Person]
list = [("Frank",24,[wall, door],1),("Ann",45,[window],0),
("Claudia",44,[window, bed],2), ("Pedro",77,[wall],1)]

error:表达式中出现语法错误(意外的";",可能是由于布局不正确(

字符串必须在"字符串";。此外,您不能将类型命名为Num,它已经是Prelude中的保留词

您可以将其重命名为Numb

type Home = (Homename)
type Homename = String
type Person = (Name, Age, [Home], Numb)
type Name = String
type Age = Integer
type Numb = Integer
list :: [Person]
list = [("Frank"  ,24,["wall", "door"],1),
("Ann"    ,45,["window"],0),
("Claudia",44,["window", "bed"],2),
("Pedro"  ,77,["wall"],1)]

您还需要使函数的主体在所有行中的标头之后启动。

这将给您一个错误,因为第二行从作为标题的位置0开始。

list :: [Person]
list = [("Frank"  ,24,["wall", "door"],1),
("Ann"    ,45,["window"],0),
("Claudia",44,["window", "bed"],2),
("Pedro"  ,77,["wall"],1)]

最新更新