Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自定义类型的Haskell派生显示_Haskell_Instance_Show - Fatal编程技术网

自定义类型的Haskell派生显示

自定义类型的Haskell派生显示,haskell,instance,show,Haskell,Instance,Show,如何告诉haskell,当对代数类型的变量列表调用show时,应在每行后面插入“\n” type Customer = (Int, Int, [Int]) 我试着这样做: instance of Show Customer where show x = x ++ "\n" 但显然,我只能为“数据…”之类的东西创建这样的实例。我怎样才能解决这个问题 我需要仅为客户列表导出Show,这样当我显示它时,输出很容易读取,每行一个客户。要仅在不同的行上显示,不要更改Show,只需执行取消行(地图显示客

如何告诉haskell,当对代数类型的变量列表调用
show
时,应在每行后面插入“\n”

type Customer = (Int, Int, [Int])
我试着这样做:

instance of Show Customer where
show x = x ++ "\n"
但显然,我只能为“数据…”之类的东西创建这样的实例。我怎样才能解决这个问题


我需要仅为客户列表导出
Show
,这样当我显示它时,输出很容易读取,每行一个客户。

要仅在不同的行上显示,不要更改
Show
,只需执行
取消行(地图显示客户列表)
。这将显示它们中的每一个,然后将它们与中间的换行符放回一起


但是,您要求更改
类型
同义词的显示,以下是您的选项:

show
用于数据的基本序列化。如果你想做一些不同的事情,你有几个选择:

  • 在此实例中编写一个执行此操作的函数
  • 制作您自己的显示类,并在其
    Display
    功能中定义您喜欢的布局方式
  • 使用
    newtype
    包装数据
  • 声明您自己的客户类型
  • 稍后添加换行符

    类型Customer=(Int,Int,[Int])

  • 示例1

    displayC :: Customer -> String
    displayC = (++"\n").show
    
    {-# LANGUAGE TypeSynonymInstances,  FlexibleInstances #-}
    class Display a where
      display :: a -> String
    
    instance Display Customer where
       display x = show x ++ "\n"
    
    newtype Cust = Cust Customer
    displayCust (Cust c) = show c ++ "\n"
    
    data CustomerTup = CTup Int Int [Int]
    displayCTup (CTup a b cs) = show (a,b,cs) ++ "\n"
    
    stuff = unlines $ map show  [(1,2,[3,4]),(5,6,[7,8,9])]
    
    示例2

    displayC :: Customer -> String
    displayC = (++"\n").show
    
    {-# LANGUAGE TypeSynonymInstances,  FlexibleInstances #-}
    class Display a where
      display :: a -> String
    
    instance Display Customer where
       display x = show x ++ "\n"
    
    newtype Cust = Cust Customer
    displayCust (Cust c) = show c ++ "\n"
    
    data CustomerTup = CTup Int Int [Int]
    displayCTup (CTup a b cs) = show (a,b,cs) ++ "\n"
    
    stuff = unlines $ map show  [(1,2,[3,4]),(5,6,[7,8,9])]
    
    (注意,您应该说
    实例显示客户
    ,而不是
    实例显示客户

    示例输出:

    *Main> display ((3,4,[5,6])::Customer)
    "(3,4,[5,6])\n"
    
    但是,这些语言扩展应该谨慎使用

    示例3

    displayC :: Customer -> String
    displayC = (++"\n").show
    
    {-# LANGUAGE TypeSynonymInstances,  FlexibleInstances #-}
    class Display a where
      display :: a -> String
    
    instance Display Customer where
       display x = show x ++ "\n"
    
    newtype Cust = Cust Customer
    displayCust (Cust c) = show c ++ "\n"
    
    data CustomerTup = CTup Int Int [Int]
    displayCTup (CTup a b cs) = show (a,b,cs) ++ "\n"
    
    stuff = unlines $ map show  [(1,2,[3,4]),(5,6,[7,8,9])]
    
    示例4

    displayC :: Customer -> String
    displayC = (++"\n").show
    
    {-# LANGUAGE TypeSynonymInstances,  FlexibleInstances #-}
    class Display a where
      display :: a -> String
    
    instance Display Customer where
       display x = show x ++ "\n"
    
    newtype Cust = Cust Customer
    displayCust (Cust c) = show c ++ "\n"
    
    data CustomerTup = CTup Int Int [Int]
    displayCTup (CTup a b cs) = show (a,b,cs) ++ "\n"
    
    stuff = unlines $ map show  [(1,2,[3,4]),(5,6,[7,8,9])]
    
    甚至更好

    data CustomerRec = CRec {custno::Int, custAge::Int, custMeasurements::[Int]}
      deriving Show
    displayCRec r = show (custno r,custAge r,custMeasurements r) ++ "\n"
    
    您甚至可以坚持使用
    Show
    实例的做事方式。
    数据
    方法很好,因为有更多的类型安全性,并且记录类型可以防止您犯微小的错误位置错误

    示例5

    displayC :: Customer -> String
    displayC = (++"\n").show
    
    {-# LANGUAGE TypeSynonymInstances,  FlexibleInstances #-}
    class Display a where
      display :: a -> String
    
    instance Display Customer where
       display x = show x ++ "\n"
    
    newtype Cust = Cust Customer
    displayCust (Cust c) = show c ++ "\n"
    
    data CustomerTup = CTup Int Int [Int]
    displayCTup (CTup a b cs) = show (a,b,cs) ++ "\n"
    
    stuff = unlines $ map show  [(1,2,[3,4]),(5,6,[7,8,9])]
    
    甚至

    morestuff = unlines [show (1,2,[3,4]), 
                         show (5,6,[7,8,9]),
                         "are all more numbery than",
                         show (True,4,False)]
    

    AndrewC极好的答案的一点补充:

    6.编写一个函数,将新行添加到类中任何类型的文本表示中
    Show

    display :: Show a => a -> String
    display = flip shows "\n"
    
    例如:

    > display (2, 3, [5, 7, 11])
    "(2,3,[5,7,11])\n"
    

    那是行不通的;您仍然无法使
    Customer
    成为
    Display
    类的实例,因为
    Customer
    是一个类型同义词。您是如何在两分钟内写出此内容的o@Tarrasch我写了一个程序。“你们知道,递归非常方便。”塔拉什只是开玩笑,我写它的时间更长了。单击“编辑的时间”链接以查看每次编辑的计时。事实上,我确实在几年前写了一个程序,但那是在他们发明废弃样板和模板haskell之前。它解析了富文本格式的文件,其中文本的样式/颜色表示它在程序、程序编写程序还是程序编写程序中代表了一个值。希望我能投票两次,一次是正确的建议,一次是正确的答案。这是一个简洁的代码。我原以为应该是1(b),所以我重新编号了,但现在我明白你的意思不同了,将我的编辑回滚到选项6。它没有按照我的预期工作。列表仍以相同的方式显示,并粘贴了“\n”between@user1166935什么意思?是否确实要将要打印的字符串写入终端。尝试:
    display'x=putStr(display x)