を
import System.IO
import Data.Char
import System.Environment
main = do
args <- getArgs
progName <- getProgName
content <- readFile $ head args
putStrLn $ show $ getWordsInfo content
getWordsInfo = let
wordList = filter (\x -> length x > 2 && all isAlpha x) . words
in foldl foldingFunction 0 wordList
where foldingFunction acc tWord = acc + length tWord
得ます。
間違っている本の唯一の行は次のとおりです。
let wordList = filter (\x -> length x > 2 && all isAlpha x) . words
エラーメッセージがあなたはそれが十分な引数に適用されていないワードリスト呼び出すとき、それはリストのリストを期待していると言っています代わりに文字列を受け取り、リストのリストを生成する関数が与えられています。ですから、wordList関数に入力文字列を渡すだけです。
あなたはそれを二つの方法を書き換えることができます。
最初は明示的に引数を指定することである:
getWordsInfo xs = let wordList = filter (\x -> length x > 2 && all isAlpha x) (words xs)
in foldl foldingFunction 0 wordList
where foldingFunction acc tWord = acc + length tWord
第二は、letバインディングではない時点自由ビットを維持することである:
getWordsInfo = foldl foldingFunction 0 . filter (\x -> length x > 2 && all isAlpha x) . words
where foldingFunction acc tWord = acc + length tWord
あなたの折り畳み機能は、各単語の長さを取り、それらを合計します。これは、リストにマッピングして長さを取ってから、リストを合計することで簡略化できます。
getWordsInfo = sum . map length . filter (\x -> length x > 2 && all isAlpha x) . words
そして、そのラインが少し長くなっているので、私たちは、おそらく最終的には私たちを与える別の定義にそれのいくつかを考慮する必要があります。
import Data.Char (isAlpha)
getWordsInfo = sum . map length . filter isLongWord . words
where isLongWord x = length x > 2 && all isAlpha x
使用法:
λ> getWordsInfo "apple banana orange a a b b punctuation!!"
17
λ> getWordsInfo "aa bb cc"
0
λ> getWordsInfo "!!!"
0
λ> getWordsInfo "apple"
5
λ>
あなたは何ですかgetWordsInfo関数で達成しようとしていますか?すべての英数字の長さが2より大きい単語の文字数を取得しますか?もしそうなら、本当にfoldlを使う必要はありません。あなたはsumを使うことができます。 – Zpalmtree
折りたたみ機能では、Intを返す他のものを行うことができますが、なぜこれがコンパイルされないのだろうと思っています.... – momi