實際上,我正在嘗試實作一個自定義資料型別,它是字串類的子集(例如,我可以對錯誤輸入進行一些型別檢查)。但我終其一生都找不到能解釋每種型別關鍵字含義的地方。根據這里:Haskell Docs這些是 7 個基本的:Eq、Ord、Enum、Ix、Bounded、Read 和 Show。除了列印陳述句所需的 Show 和布爾樣式比較檢查所需的 Eq 之外,我對其他 5 個并不完全確定,谷歌并沒有提供太多幫助。所以我希望你們都可以闡明一些觀點,也許可以為我指明正確的方向。
所以問題:
- 這 7 個基本派生是什么/添加它們有什么作用?
- 在 ghci 中有沒有辦法可以運行類似
derives Stringor的東西derives "abc"?
這是我正在處理的代碼。本質上,我剛剛創建了這種 Card 資料型別,如您所見,它只是一個執行更嚴格引數檢查的字串。而我想要做的是將它傳遞給以前接受 3 個字串作為引數(現在是 3 張卡片)的匹配項。但是為了使用字串操作語法,比如將其分解為字串列,我相信我需要識別適當的派生關鍵字,以便它以這種方式運行。
match :: Card -> Card -> Card -> Bool
match [] [] [] = True
match [a] [b] [c] = (a == b && b == c)
|| (a /= b && b /= c && a /= c)
match (a:as) (b:bs) (c:cs) = match [a] [b] [c]
&& match as bs cs
data Card = Card String
deriving (Show,Eq)
card :: String -> Card
card x | length x /= 4 = error "Card input must be 4 characters."
| (x!!0 /= 'o') && (x!!0 /= 's') && (x!!0 /= 'd') = error "char 0 (shape) must be o s or d."
| (x!!1 /= '1') && (x!!1 /= '2') && (x!!1 /= '3') = error "char 1 (number) must be 1 2 or 3."
| (x!!2 /= 'r') && (x!!2 /= 'p') && (x!!2 /= 'g') = error "char 2 (color) must be r p or g."
| (x!!3 /= 'f') && (x!!3 /= 's') && (x!!3 /= 'o') = error "char 3 (shade) must be f s or o."
| otherwise = Card x
(更新示例 #1)
data Card = Card Shape Number Color Shade deriving (Show, Eq)
data Shape = Oval | Squiggle | Diamond deriving Eq
instance Show Shape where
show Oval = "Oval"
show Squiggle = "Squiggle"
show Diamond = "Diamond"
data Number = One | Two | Three deriving Eq
instance Show Number where
show One = "One"
show Two = "Two"
show Three = "Three"
data Color = Red | Pink | Green deriving Eq
instance Show Color where
show Red = "Red"
show Pink = "Pink"
show Green = "Green"
data Shade = Full | Half | Empty deriving Eq
instance Show Shade where
show Full = "Full"
show Half = "Half"
show Empty = "Empty"
shape :: Char -> Either ShapeError Shape
shape 'o' = Right Oval
shape 's' = Right Squiggle
shape 'd' = Right Diamond
shape x = Left (NotOSD x)
number :: Char -> Either NumberError Number
number '1' = Right One
number '2' = Right Two
number '3' = Right Three
number x = Left (Not123 x)
color :: Char -> Either ColorError Color
color 'r' = Right Red
color 'p' = Right Pink
color 'g' = Right Green
color x = Left (NotRPG x)
shade :: Char -> Either ShadeError Shade
shade 'f' = Right Full
shade 's' = Right Half
shade 'o' = Right Empty
shade x = Left (NotFSO x)
card :: String -> Either SetError Card
card [shp, n, c, shd] = Card <$> shape shp <*> number n <*> color c <*> shade shd
card x = Left (NotCard x)
data CardError = NotCard String
instance Show CardError where
show (NotCard x) = "Card must be 4 characters long, not " show x
data SetError = CardError | ShapeError | NumberError | ColorError | ShadeError deriving Eq
data ShapeError = NotOSD Char
instance Show ShapeError where
show (NotOSD x) = "Shape must be o, s, or d, not " show x
data NumberError = Not123 Char
instance Show NumberError where
show (Not123 x) = "Number must be 1, 2, or 3, not " show x
data ColorError = NotRPG Char
instance Show ColorError where
show (NotRPG x) = "Color must be r, p, or g, not " show x
data ShadeError = NotFSO Char
instance Show ShadeError where
show (NotFSO x) = "Shade must be f, s, or o, not " show x
當前誤差
match.hs:84:34:
Couldn't match type ‘ShapeError’ with ‘SetError’
Expected type: Either SetError Shape
Actual type: Either ShapeError Shape
In the second argument of ‘(<$>)’, namely ‘shape shp’
In the first argument of ‘(<*>)’, namely ‘Card <$> shape shp’
match.hs:84:48:
Couldn't match type ‘NumberError’ with ‘SetError’
Expected type: Either SetError Number
Actual type: Either NumberError Number
In the second argument of ‘(<*>)’, namely ‘number n’
In the first argument of ‘(<*>)’, namely
‘Card <$> shape shp <*> number n’
match.hs:84:61:
Couldn't match type ‘ColorError’ with ‘SetError’
Expected type: Either SetError Color
Actual type: Either ColorError Color
In the second argument of ‘(<*>)’, namely ‘color c’
In the first argument of ‘(<*>)’, namely
‘Card <$> shape shp <*> number n <*> color c’
match.hs:84:73:
Couldn't match type ‘ShadeError’ with ‘SetError’
Expected type: Either SetError Shade
Actual type: Either ShadeError Shade
In the second argument of ‘(<*>)’, namely ‘shade shd’
In the expression:
Card <$> shape shp <*> number n <*> color c <*> shade shd
match.hs:85:16:
Couldn't match expected type ‘SetError’
with actual type ‘CardError’
In the first argument of ‘Left’, namely ‘(NotCard x)’
In the expression: Left (NotCard x)
Failed, modules loaded: none.
uj5u.com熱心網友回復:
撰寫 Haskell 時,不要害怕定義很多型別和很多小函式。小函式更容易理解,可以通過各種方式組合來定義更大的函式。
你關心型別類...
為了讓您立即解決問題,該Show實體僅用于String從某個值產生 a。Eq用于允許將兩個相同型別的值與==. 您可以為其派生實體的其他類(沒有特定于 GHC 的擴展)是
ReadOrdEnumBounded
Read類似于 的倒數Show,但一般不使用。(我在下面展示的決議器比實體更容易撰寫Read,并且對于您將定義的大多數型別,您將無法使用派生Read實體。)其他三個可能與您程式的其他部分相關,但不適用于您問題中的任何代碼。
派生Show實體基本上只是將資料建構式名稱轉換為字串,但作為一種良好的風格,該字串應該是有效的 Haskell 代碼。派生Eq實體只檢查兩個資料建構式是否相同。接受引數的資料建構式的定義是遞回派生的:show (Card x)將使用"Card"and定義show x,而Card c1 == Card c2將定義為c1 == c2。
不過,您的大部分問題都是關于模式匹配(如評論中所述),這與型別類無關。
...或型別?
讓我們從你的四個基本概念開始:形狀、數字、顏色和陰影。為它們中的每一個定義一個單獨的型別。以下是Shape作為示例的型別:
data Shape = ShapeO | ShapeS | ShapeD deriving (Show, Eq)
在可能的情況下為資料建構式使用描述性名稱(O實際上是 的縮寫Oval?如果是,請使用Oval),并記住名稱必須是全域唯一的(無論如何,在模塊內),因此您不能O同時使用Shape和Shade。有時,從建構式名稱派生的字串對您的實體來說就足夠了,Show有時則不然。如果沒有,與其定義Show實體,不如撰寫自己的Thing -> String函式。
您是派生Show實體還是手動撰寫它可能取決于您選擇的資料建構式名稱以及您希望從值中獲得什么字串。派生Eq實體幾乎肯定就足夠了,因為它們不依賴于您用于建構式的名稱。
(順便說一句,Number這很棘手,因為您不能使用1,2或3作為資料建構式名稱。除非您需要對數字進行數學運算,否則您可能會考慮類似data Number = One | Two | Three而不是過于寬泛的data Number = Number Int.)
現在您的Card型別可以簡單地組合您的四種屬性型別,而不僅僅是原始字串的包裝器。
data Card = Card Shape Number Color Shade deriving (Show, Eq)
(但關于Show,任何具有自己的字串轉換函式的屬性都可能迫使您撰寫Card -> String自己的函式而不是依賴于Show實體。)
決議:字串到值
您需要將字符決議為適當值的函式,而不僅僅是檢查它是否可以表示適當的值。有時這是可能的(for'o'),ShapeO有時'x'不是決定程式是否需要終止。例如,Shapeerror
shape :: Char -> Either String Shape
shape 'o' = Right ShapeO
shape 's' = Right ShapeS
shape 'd' = Right ShapeD
shape x = Left $ "Shape must be o, s, or d, got show x
shape回傳一個Right或值的事實Left通常足以讓呼叫者決定下一步該做什么;包裹的錯誤資訊Left更多是為了向用戶報告錯誤。
一旦擁有了各個屬性的所有決議器,a 的決議器Card就變得幾乎是微不足道的,這要歸功于型別的Applicative實體Either String。
card :: String -> Either String Card
card [shp, n, c, shd] = Card <$> shape shp <*> number n <*> color c <*> shade shd
card x = Left $ "Card must e 4 characters long, got " show x
如果任何屬性產生一個Left值,card將回傳Left第一個決議失敗的屬性的值。只有當輸入字串正好有 4 個正確字符時,才會char產生一個Card用Right.
您可能還決定定義專用的錯誤型別,而不是使用任意字串。例如,
data ShapeError = NotOSD Char
errorMessage :: ShapeError -> String
errorMessage (NotOSD x) = "Shape must be o, s, or d, not " show x
然后
shape :: String -> Either ShapeError Shape
shape 'o' = Right ShapeO
shape 's' = Right ShapeS
shape 'd' = Right ShapeD
shape x = Left (NotOSD x)
基本上,在錯誤型別中捕獲有關錯誤的資訊,并在需要時讓其Show實體生成帶有該資訊的人類可讀字串。
如果它們的部分相等,則事物是相等的
match同樣微不足道,使用Eq派生的實體Card,它派生自Eq所有屬性型別的實體。
match :: Card -> Card -> Card -> Bool
match c1 c2 c3 = c1 == c2 && c2 == c3 || (c1 /= c2 && c2 /= c3 && c3 /= c1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532472.html
上一篇:無法將預期型別`t0->t'與實際型別`Rat'匹配
下一篇:使用遞回方案的格路徑演算法
