さて、このコードは」勝ちましたtコンパイルします。
まず第一に、あなたのpunct
機能では、このコンパイルエラーが二度起こる:それは一度に一つの問題をコンパイルしない理由のタックルしてみましょう
Foo.hs:7:11: error:
• Couldn't match expected type ‘(Char -> Bool) -> Bool’
with actual type ‘Char’
• The function ‘c’ is applied to one argument,
but its type ‘Char’ has none
In the expression: c isDigit
In a stmt of a pattern guard for
an equation for ‘punct’:
c isDigit
コンパイラはここであなたを伝えようとしていることということです何
あなたは明らかに別の方法を意味していたときに、 "isDigit
"という引数を渡して "関数としてc
を呼び出します"と書いた。だから、最初に我々はそれらを交換:固定それと
punct :: Char -> Bool
punct c | c `elem` ['.', ',', '?', '!', ':', ';', '(', ')'] = True
| isDigit c = True
| isAlpha c = True
| otherwise = False
、実際にあなたの最初のformat
関数がコンパイルされます。
format1 :: String -> String
format1 s = intercalate "" [if (punct c) then (c:" ") else [c] | c <- s]
そしてまた、宣伝として動作しているようです:
*Foo> format1 "a.b_c"
"a . b _c "
さて、今度はその第二のformat
の機能はどうですか?まあ、最初はそれがコンパイルされません。
Foo.hs:18:12: error:
• Couldn't match type ‘[Char] -> [[Char]]’ with ‘[Char]’
Expected type: String
Actual type: [Char] -> [[Char]]
• Probable cause: ‘map’ is applied to too few arguments
In the expression: map (\ c -> if punct c then (c : " ") else [c])
In an equation for ‘format’:
format s = map (\ c -> if punct c then (c : " ") else [c])
をコンパイラを使用すると、右側にs
を使うのを忘れていました(「考えられる原因」メッセージで)ここに、あなたを語っている、と書かれている必要があります。
を
format s = map (\c -> if punct c then (c:" ") else [c]) s
しかし、コンパイルされませんイベント:あなたの結果はString
されているべきであると言っている(ただし、エラーメッセージが今異なっている)
Foo.hs:18:12: error:
• Couldn't match type ‘[Char]’ with ‘Char’
Expected type: String
Actual type: [[Char]]
• In the expression:
map (\ c -> if punct c then (c : " ") else [c]) s
In an equation for ‘format’:
format s = map (\ c -> if punct c then (c : " ") else [c]) s
(つまり、 Char
、または[Char]
)のリストが、その代わりにはそれがあなたの代わりにの文字に、列に各文字を回したので、これは、[[Char]]
)
です(String
秒のリストでした。この問題を解決する一つの方法文字に各文字を有効にすることです:
format s = map (\c -> if punct c then ' ' else c) s
もう一つの方法は、単一の文字列に文字列のリストを結合するためにあなたが前に行ったようintercalate ""
(またはconcat
)を適用することです。
format s = intercalate "" $ map (\c -> if punct c then " " else [c]) s
「*上記のものを削除する」とはどういう意味ですか?句読点? –
@Willem Van Onsem:句読点で真を返す記号 – Madalina
「それは機能していません」と言ったら、何を意味するのかを明記してください。このコードはコンパイルされないので、「コンパイルできません」と言ってエラーメッセージを出してください。 –