2009-10-03 5 views
0

에서의 중첩 된 이름은 내가 가진 :패턴 일치 데이터 유형 및 하스켈

data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord) 

그리고

data Term = Color | ... 
data Bag = Bag { 
color :: Color 
... 
} 

가 지금은 주어진 용어가이 있는지 확인하기 위해 패턴 일치 할 수 있도록하려면 색상이 맞는지 확인하십시오. "값"(파란색/녹색 ...)입니다. 이 같은 것은 :

func :: Term -> Bag -> Bool 
func (c :: Color) bag = (color bag) == c 

하지만 (c :: Color)는 작동하지 않는 것 같습니다.

+7

이 다소 일반적인 초보자 오해 것 같다 Term = Color')은 완전히 다른 네임 스페이스에서 완전히 관련이 없습니다. – ephemient

+1

@ephemien 그 대답을 받아 들였습니다. –

답변

8
data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord) 

data Term = Color Color | Trash 

data Bag = Bag { 
    color :: Color 
} 

func (Color x) bag = (color bag) == x 

-- With the above, a call of func Trash something will fail. 
-- (unexhastive pattern match). You can add 

func Trash bag = False 

-- or 

func _ _ = False 

-- and it will work all time. 
2

여기는 다소 단어가 더 많은 계정이며, sdcvvc의 콘텐츠와 다르지 않습니다.

 
data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord,Show) 
data Size = Small | Medium | Large deriving (Eq, Ord, Show) 

data Term = TColor Color | TSize Size | Trash deriving Show 
data Bag = Bag {color :: Color , size :: Size} deriving Show 

sdcvvc와는 달리 "TColor Color"를 사용합니다. 유형 검사기가 이미 설정된 유형이고 다른 유형의 새로운 생성자 인 경우 철자가 같아도 알 수 있습니다. 혼란스럽지 않은 점이 많습니다. "Color Color"와 같은 것은 요즘 드문 일이 아니지만 Bird의 "Intro to FP with Haskell"과 같은 오래된 책에서 그는 이런 종류의 일을하지 않을 것입니다. Haskeller의 이점처럼 "\ file -> readFile file"과 같은 장점이 있지만 잠재적으로 혼란스럽고 꽤 최근 인 것 같습니다. 이전에는 \ x -> readFile x 또는 뭔가 였을 것입니다.

 
theMrsThatcher :: Bag 
theMrsThatcher = Bag Blue Large 

theMrsRobinson :: Bag 
theMrsRobinson = Bag {color = Green, size = Small} -- to use the other syntax 

colorCheck :: Term -> Bag -> Bool 
colorCheck (TColor c) b = True 
colorCheck (TSize s) b = False -- as sdcvvc says, this and 
colorCheck Trash b = False  -- the next clause can be replaced by 
           -- colorCheck _ _ = False 

또한 가방은 관련이 없다는 점에 유의하십시오. 중급 유형 용어가 필요한 이유가 무엇인지 분명하지 않습니다.

 
colorTest :: Color -> Bag -> Bool 
colorTest c b = color b == c 

colorCheckTest :: Term -> Bag -> Bool 
colorCheckTest (TColor c) b = color b == c 
colorCheckTest (TSize s) b = False -- as above, the last clauses are 
colorCheckTest Trash b = False  -- wordier than need be since any 
             -- but the first pattern is a loser. 

결과 : 데이터`에서 ((데이터 Color``에서) Color` '라는 이름의 종류와 Color`'라는 이름의 생성자 :

 
*Main> colorCheck (TColor Blue) theMrsRobinson 
True 
*Main> colorCheck (TColor Blue) theMrsThatcher 
True 
*Main> colorCheckTest (TColor Blue) theMrsRobinson 
False 
*Main> colorCheckTest (TColor Blue) theMrsThatcher 
True 
*Main> colorTest Blue theMrsThatcher 
True 
*Main> colorTest Blue theMrsRobinson 
False