あなたは機能split'
と機能spilt'
を宣言。タイプミスを訂正してください。
split' :: RawText -> [SingleWord]
spilt' [] = [] -- the first typo is here
split' xs
| xs == [] = []
| isBlank (head xs) = split' xs
| otherwise = waitForBlank xs : spilt' (tail xs) -- here is the second typo
メッセージを説明するために、それはあなたが宣言した2つの異なる機能にコードを分割するのが最善です:あなたはsplit'
に再帰するとき
split' :: RawText -> [SingleWord]
split' xs
| xs == [] = []
| isBlank (head xs) = split' xs
| otherwise = waitForBlank xs : spilt' (tail xs)
-- spilt' :: [a] -> [a] (or something like this)
spilt' [] = []
は今、あなたはspilt'
を呼び出します。宣言した関数spilt'
は空でないリストを処理しないため、例外がスローされます。さらにノートで
、あなたがタイプミスを修正した場合、あなたは二度空のリストを処理する必要はありません。
また
import Prelude hiding (split)
split :: RawText -> [SingleWord]
split [] = []
split xs
-- | xs == [] = [] this case is matched by the pattern
-- above and thus not needed
| isBlank (head xs) = split' xs
| otherwise = waitForBlank xs : split (tail xs)
、パターンリストに一致したときに、あなたが明示的にパターンマッチすることができます
split [email protected](x:xs)
| isBlank x = split' xs
| otherwise = waitForBlank s : split' xs
しかし、どういうわけか、この機能はまだSE:明示的に拳cons
アプリケーションを書き出すことによってhead
とリストのtail
に対して、すべての手紙を飛ばすより良い方法がなければならない。何が私たちを助けることができるかを見るライブラリ関数を見てみましょう。あなたはそれらを見つけることができますhere:
-- drops letters from RawText while a certain predicate holds
dropWhile :: (a -> Bool) -> [a] -> [a]
-- takes letters form RawText while a certain predicate holds
takeWhile :: (a -> Bool) -> [a] -> [a]
これらはかなり有望です。我々はまた、すべての非空白文字をスキップしてsplit
機能をより効率的に行うことができますdropWhile
を使用
waitForBlank xs = takeWhile (not . isBlank) xs
-- or, pointfree:
waitForBlank = takeWhile (not . isBlank)
:今、私たちはとwaitForBlank
を書き換えることができます。スペースはdropWhile
の部分に含まれており、明示的に削除する必要があることに注意してください。
split :: RawText -> [SingleWord]
split [] = []
split xs = waitForBlank xs : split (dropUntilBlank xs)
waitForBlank xs = takeWhile (not . isBlank) xs
dropUntilBlank xs = tail (dropWhile (not . isBlank) xs)
最後の部分もbreak
を使用して組み合わせることができます:
split :: RawText -> [SingleWord]
split [] = []
split xs = word : split' rest
where
(word, (space:rest)) = break isBlank xs
この答えは、すべての単語が区切られていることを前提としてい
split :: RawText -> [SingleWord]
split [] = []
split xs = waitForBlank xs : split (dropUntilBlank xs)
-- With dropUntilBlank defined as
dropUntilBlank xs = tail (dropWhile (not . isBlank) xs)
-- without the call of tail, dropUntilBlank would keep the space in between the words:
dropWhile (not . isBlank) "a word" => " word"
-- note the extra space: ^^^
-- using tail on this now produces the correct word:
tail (dropWhile (not . isBlank) "a word") = "word"
は今、結果がきれいに見えます単一のスペース。複数の空白の場合はを書き換えて、
dropWhile isBlank
の代わりに
tail
を置き換えて、複数の空白を除外する必要があります。
有効にする警告は「spilt' 'のための署名を逃す」のようなメッセージを生成し、タイプミス' spilt'をスポッティングでここに役立っていると思います。 – chi