이유 ghci는 I가 패턴 매칭을 통해 제작이 함수 matchInt
의 형태 서명에 equality type 제약을 나열 않는다 : 단순 데이터 생성자를 사용할 때패턴 일치로 생성 된 함수가 Eq 유형 제약 조건을 가지고 있지만 데이터 생성자를 사용하지 않는 이유는 무엇입니까?
$ ghci
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Prelude> :{
Prelude| matchInt 1 = 3
Prelude| matchInt _ = 4
Prelude| :}
Prelude> matchInt 1
3
Prelude> matchInt 22
4
Prelude> :t matchInt
matchInt :: (Eq a, Num a, Num p) => a -> p
는 어떠한 참가 형 제약이 없다. Z의
실제로$ ghci
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Prelude> data Z = Y Int
Prelude> :{
Prelude| matchDataInt (Y 1) = 3
Prelude| matchDataInt _ = 4
Prelude| :}
Prelude> matchDataInt (Y 1)
3
Prelude> matchDataInt (Y 22)
4
Prelude> :t matchDataInt
matchDataInt :: Num p => Z -> p
인스턴스 비교할 수없는 수
Prelude> Y 22 == Y 33
<interactive>:11:1: error:
• No instance for (Eq Z) arising from a use of ‘==’
• In the expression: Y 22 == Y 33
In an equation for ‘it’: it = Y 22 == Y 33
를 그래서 다시, 이유는 무엇입니까 matchInt
기능 목록 유형의 제약 조건으로 평등하지만 기능 matchDataInt
?
이 question과 관련됩니다. 그러나 matchInt
에 대한 동등성 테스트가 필요한 경우 matchDataInt
은 왜 필요하지 않습니까? 그리고 여기에서 필자는 핵심 포인트에 도달했다 : matchInt
및 matchDataInt
모두 패턴 일치가 작동하도록 테스트해야합니까?