1

ここに私のADTがあります:

type Color = String
type Hight = Integer
type Width = Integer
type Des = String -- description 

data Shape = Shape Color [(Hight, Width)] Des
         deriving(Show)

という関数を定義したいと思いますconfirm:

confirm::Restriction->Shape->Bool

どこ:

false::Restriction -- the restriction fails

greater::Integer->Restriction --height or width is greater than given number

Restrictionconfirmfalseおよびの定義についてサポートが必要ですgreater

4

2 に答える 2

2

データ型の制限は である必要があります Shape -> Bool。の代わりに をconfirm使用できます。Falsefalse

名前を変更greaternotTooBigたのは、データに問題がない場合に当てはまるためです。そのほうが理にかなっている気がします。

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

于 2012-10-22T16:16:50.487 に答える
1

他の答えを拡張する:

type Restriction = (Shape -> Bool)

false :: Restriction
false = const False

greater :: Integer -> Restriction
greater r (Shape _ dims _) = any (\(h,w) -> h > r || w > r) dims

confirm :: Restriction -> Shape -> Bool
confirm = id
于 2012-10-22T17:09:22.643 に答える