从数据类型打印列表
Print List out of a datatype
我正在研究一种数据类型,它涉及一个人的名字和姓氏、他们的方向和他们居住的城市。我制作了一个函数来打印这个,但我很确定我可以以某种方式实例化 Show 或创建另一个 class 并实例化它。我有一个名为 Direction:
的数据类型
type Direction = (Person, Dir, City)
type Name = String
type Surname = String
type City = String
data Person = Per Name Surname
data Dir = Street String Int | House String
这两个元素:
dirJon:: Direction
dirJon = (Per "Jon" "Prieto", House "Enea", "Orio")
dirMiren:: Direction
dirMiren = (Per "Miren" "Artola", Street "Aldamar" 15, "Donostia")
所以我想出的函数是:
write:: [Direction] -> IO()
write [] = return ()
write ((Per a b,Street c d, e):cs) = do
putStrLn ( a ++ ' ': b)
putStrLn ("c/" ++ c ++'c':show d)
putStrLn e
write cs
write ((Per a b,House c, e):cs) = do
putStrLn ( a ++ ' ': b)
putStrLn ("House " ++ c)
putStrLn e
write cs
这会导致正确的实现,这意味着如果我调用 write [dirJon,dirMiren]
会正确打印:
Jon Prieto
casa Enea
Orio
Miren Artola
c/Aldamarc15
Donostia
(换句话说,我不想把它写成论文)
所以如果有人能帮助我以某种方式制作它的实例,因为我已经尝试制作一个使用 [Direction] 作为参数的 class 但是因为它是 type 而不是 数据 我做不到。
您不能为类型别名定义实例。我只想定义一个合适的数据类型。
type Name = String
type Surname = String
type City = String
data Person = Per Name Surname
data Location = Street String Int | House String
data Address = Address Person Location City
addrJon, addrMiren :: Address
dirJon = Address (Per "Jon" "Prieto") (House "Enea") "Orio"
dirMiren = Address (Per "Miren" "Artola") (Street "Aldamar" 15) "Donostia"
instance Show Person where
show (Per x y) = x ++ " " ++ y
instance Show Location where
show (Street name number) = "c/" ++ name ++ "c" ++ show number
show (House name) = "House " ++ name
instance Show Address where
show (Address p l c) = show p ++ "\n" ++ show l ++ "\n" ++ c
write :: [Address] -> IO()
write [] = return ()
write (a:as) = putStrLn (show a) >> write as
-- write = traverse print
我正在研究一种数据类型,它涉及一个人的名字和姓氏、他们的方向和他们居住的城市。我制作了一个函数来打印这个,但我很确定我可以以某种方式实例化 Show 或创建另一个 class 并实例化它。我有一个名为 Direction:
的数据类型type Direction = (Person, Dir, City)
type Name = String
type Surname = String
type City = String
data Person = Per Name Surname
data Dir = Street String Int | House String
这两个元素:
dirJon:: Direction
dirJon = (Per "Jon" "Prieto", House "Enea", "Orio")
dirMiren:: Direction
dirMiren = (Per "Miren" "Artola", Street "Aldamar" 15, "Donostia")
所以我想出的函数是:
write:: [Direction] -> IO()
write [] = return ()
write ((Per a b,Street c d, e):cs) = do
putStrLn ( a ++ ' ': b)
putStrLn ("c/" ++ c ++'c':show d)
putStrLn e
write cs
write ((Per a b,House c, e):cs) = do
putStrLn ( a ++ ' ': b)
putStrLn ("House " ++ c)
putStrLn e
write cs
这会导致正确的实现,这意味着如果我调用 write [dirJon,dirMiren]
会正确打印:
Jon Prieto
casa Enea
Orio
Miren Artola
c/Aldamarc15
Donostia
(换句话说,我不想把它写成论文)
所以如果有人能帮助我以某种方式制作它的实例,因为我已经尝试制作一个使用 [Direction] 作为参数的 class 但是因为它是 type 而不是 数据 我做不到。
您不能为类型别名定义实例。我只想定义一个合适的数据类型。
type Name = String
type Surname = String
type City = String
data Person = Per Name Surname
data Location = Street String Int | House String
data Address = Address Person Location City
addrJon, addrMiren :: Address
dirJon = Address (Per "Jon" "Prieto") (House "Enea") "Orio"
dirMiren = Address (Per "Miren" "Artola") (Street "Aldamar" 15) "Donostia"
instance Show Person where
show (Per x y) = x ++ " " ++ y
instance Show Location where
show (Street name number) = "c/" ++ name ++ "c" ++ show number
show (House name) = "House " ++ name
instance Show Address where
show (Address p l c) = show p ++ "\n" ++ show l ++ "\n" ++ c
write :: [Address] -> IO()
write [] = return ()
write (a:as) = putStrLn (show a) >> write as
-- write = traverse print