データ型の制限は である必要があります Shape -> Bool
。の代わりに をconfirm
使用できます。False
false
名前を変更greater
しnotTooBig
たのは、データに問題がない場合に当てはまるためです。そのほうが理にかなっている気がします。
notTooBig:: Shape -> Bool
notTooBig n (Shape _ dimensions _) = all smallenough dimensions where
smallenough (h,w) = h <=n && w <= n
手段はこの_
ビットを無視します。色や説明は必要ありません。
編集:あなたにとって非常に重要なようです
confirm::Restriction->Shape->Bool
と
false::Restriction -- the restriction fails
Restriction
それでは、あなたに合ったデータ型を作りましょう:
data ARestriction = ColourR (Colour -> Bool) -- (sorry I can't resist using British spelling)
| HeightR (Height -> Bool) -- (Hight is surely a typo)
| WidthR (Width -> Bool)
| DesR (Des -> Bool)
type Restrictions = [ARestriction]
たとえば、[ColourR (=="red"), WidthR (>7)]
7 よりも幅の広い赤いものだけを許可するようにすることができます。
confirm1 :: ARestriction -> Shape -> Bool
confirm1 (ColourR check) (Shape c ds d) = check c
confirm1 (HeightR check) (Shape c ds d) = all check $ map fst ds
confirm1 (WidthR check) (Shape c ds d) = all check $ map snd ds
confirm1 (DesR check) (Shape c ds d) = check d
confirm :: Restrictions -> Shape -> Bool
confirm rs s = all (flip confirm1 s) rs
とにかく、これを次のように使用できます。
confirm [ColourR (=="red"), WidthR (>7)] (Shape "red" [(2,3),(3,4)] "square")
これはあなたに与えますTrue
。
も定義したかったのですが、最初false
に試してみましょう。true
true :: Restrictions
true = []
これは、リスト内のすべての制限が満たされているため機能します。
定義することもできます
false :: Restrictions
false = ColourR (const False)
形状の色をチェックしますが、const
おとなしく教えてくれますFalse
。