次の型定義があります。
data NumList n = NumList [Rational]
次に、コードでいくつかの NumLists を次のように定義します。
n1 = NumList [1, 2, 3]
n2 = NumList [4, 5, 6]
私がやりたいことはこれです:
n3 = n1 + n2 -- Should be [5, 7, 9]
私はこれを次のように定義しようとしました:
toList :: NumList n -> [Rational]
toList (NumList n) = n
(+) :: NumList -> NumList -> NumList
(+) x y = if length x == length y
then
zipWith (+) toList x toList y -- This probably isn't right, but I'll debug this later
else
error "NumLists must be same length to add"
私が得るエラーは次のとおりです。
Ambiguous occurrence `+'
It could refer to either `NumList.+',
defined at NumList.hs:7:5
or `Prelude.+'
Num
Prelude.+ はクラスに適用され、NumList.+ は NumList 型にのみ適用されるため、これがどのように曖昧なのかわかりません。これに関するガイダンスはありますか?また、ちょっとしたおまけ質問: パターン マッチングを使用して (+) 関数から if 式を削除する方法はありますか?