首页 > 解决方案 > 在 Haskell 中打印列表 new

问题描述

全新的haskell,我需要为每个单独的项目打印出包含在单独行中的数据

不确定如何

type ItemDescr = String
type ItemYear = Int
type ItemPrice = Int
type ItemSold = Int
type ItemSales = Int
type Item = (ItemRegion,ItemDescr,ItemYear,ItemPrice,ItemSold,ItemSales)
type ListItems = [Item]

rownumber x
 | x == 1  = ("Scotland","Desktop",2017,900,25,22500)
 | x == 2  = ("England","Laptop",2017,1100,75,82500)
 | x == 3  = ("Wales","Printer",2017,120,15,1800)
 | x == 4  = ("England","Printer",2017,120,60,7200)
 | x == 5  = ("England","Desktop",2017,900,50,45000)
 | x == 6  = ("Wales","Desktop",2017,900,20,18000)
 | x == 7  = ("Scotland","Printer",2017,25,25,3000)

showall
--print??

因此,例如在每个单独的线路显示

"苏格兰","桌面",2017,900,25,22500

接下来是下一条记录

标签: listhaskellprinting

解决方案


提示1:

像这样存储数据

items = [("Scotland","Desktop",2017,900,25,22500),
         ("England","Laptop",2017,1100,75,82500),
         ("Wales","Printer",2017,120,15,1800),
         ("England","Printer",2017,120,60,7200),
         ("England","Desktop",2017,900,50,45000),
         ("Wales","Desktop",2017,900,20,18000),
         ("Scotland","Printer",2017,25,25,3000)]

提示2:

实现这个功能

toString :: Item -> String
toString = undefined -- do this yourselves

提示 3:

尝试结合以下功能

  • unlines,已经在前奏曲中
  • toString,你刚刚写的
  • map, 不需要任何解释
  • putStrLn,甚至不确定这是否是一个真正的功能,但无论如何你都需要它。
  • ($),你可以不用这个,但它会给你加分

推荐阅读