0

だから私はhaskellを初めて使い、しばらくの間それで遊んでいます。すべてのリスト順列を出力する関数を機能させたいと考えています。私は2つの実装を書きました.1つはうまく機能し、もう1つはエラーを出しています. どんな助けでも素晴らしいでしょう。

これは最初の (実用的な) 実装です:

permute [] = [[]]
permute xs = [y| x <- xs, y <- map (x:) $ permute $ delete x xs]

これは私にエラーを与えています:

permute [] = [[]]
permute xs = map (\x -> map (x:) $ permute $ delete x xs) xs

エラーメッセージは次のとおりです。

Occurs check: cannot construct the infinite type: t0 = [t0]
Expected type: [t0]
Actual type: [[t0]]
In the expression: map (x :) $ permute $ delete x xs
In the first argument of `map', namely
`(\ x -> map (x :) $ permute $ delete x xs)'

このエラーが発生する理由を誰かが説明できれば幸いです。ありがとう

4

2 に答える 2

5

型署名を使用して、コンパイラーの作業を楽にします。

permute :: Eq a => [a] -> [[a]]、そして今、私たちは持っています:

Couldn't match type `a' with `[a]'
  `a' is a rigid type variable bound by
      the type signature for permute :: Eq a => [a] -> [[a]]
      at perm.hs:4:1
Expected type: [a]
  Actual type: [[a]] 
In the expression: map (x :) $ permute $ xs
In the first argument of `map', namely
  `(\ x -> map (x :) $ permute $ xs)'

したがって、concatMapの代わりにを使用する必要があるようですmap

permute :: Eq a => [a] -> [[a]]
permute [] = [[]]
permute xs = concatMap (\x -> map (x:) $ permute $ delete x xs) xs
于 2012-07-06T09:04:12.480 に答える