Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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
String 如何在Haskell中将构造函数转换为字符串?_String_Haskell_Constructor_Type Conversion_Show - Fatal编程技术网

String 如何在Haskell中将构造函数转换为字符串?

String 如何在Haskell中将构造函数转换为字符串?,string,haskell,constructor,type-conversion,show,String,Haskell,Constructor,Type Conversion,Show,我正在尝试以字符串格式输出一手卡片,但我不能从卡片的排名中删除单词Numeric,我更喜欢在没有辅助函数的情况下执行此操作,因为它看起来非常混乱,任何建议在没有辅助函数的情况下执行此操作。另外,当我使用display函数时,我在前奏曲中使用了putStr display x。以下是迄今为止的代码: displayCard :: Card -> String displayCard c = show (rank c) ++ " of " ++ show (suit c) ++ "\n" d

我正在尝试以字符串格式输出一手卡片,但我不能从卡片的排名中删除单词Numeric,我更喜欢在没有辅助函数的情况下执行此操作,因为它看起来非常混乱,任何建议在没有辅助函数的情况下执行此操作。另外,当我使用display函数时,我在前奏曲中使用了putStr display x。以下是迄今为止的代码:

displayCard :: Card -> String
displayCard c = show (rank c) ++ " of " ++ show (suit c) ++ "\n"

display :: Hand -> String
display Empty = "\n"
display (Add c h) = displayCard c
                    ++ display h

displayRank :: Rank -> String
displayRank (Numeric 2) = "2"
displayRank (Numeric 3) = "3"
displayRank (Numeric 4) = "4"
displayRank (Numeric 5) = "5"
displayRank (Numeric 6) = "6"
displayRank (Numeric 7) = "7"
displayRank (Numeric 8) = "8"
displayRank (Numeric 9) = "9"
displayRank (Numeric 10) = "10"
displayRank Jack = "Jack"
displayRank Queen = "Queen"
displayRank King = "King"
displayRank Ace = "Ace"
你的表演成绩是c级

您可以使用displayRank c来完成

说到类型

我更喜欢在没有助手函数的情况下执行此操作,因为它看起来非常混乱

假设你有以下类型的东西

数据卡=卡{cardRank::Rank,cardSuit::Suit} 导出Eq,显示 数据等级=数字整数|杰克|女王|国王|王牌 导出Eq,显示 数据套装=钻石|梅花|红心|黑桃 导出Eq,显示 那么编写助手函数就有意义了

displayRank::Rank->String displayRank数字n=显示n displayRank otherRank=显示otherRank displaySuit::Suit->String 显示套装 显示卡片::卡片->字符串 显示卡片等级套装=。。。 但你也可以把它们结合起来:

显示卡片::卡片->字符串 显示卡数字n套装=。。。 displayCard卡otherRank套装=。。。 拥有正确设计的助手函数并不麻烦。相反。

rank是否应该与displayRank相同?@mkriger1:我想rank是卡片类型的一个getter,但由于我们没有看到数据定义,我们无法确定。displayRank数值a=show a。但建议为您的类型编写一个Show实例。
rank ::        Card -> Rank
show ::                Rank -> String
rank ::        Card -> Rank
displayRank ::         Rank -> String