Исключение: Prelude.head: пустой список
Вот моя попытка расширить функцию:
expand :: FileContents -> FileContents -> FileContents
expand textfile infofile
= concat (combine (fst (split separators textfile)) (expand' (snd (split separators textfile)) infofile "") )
where
expand' [] _ stack = []
expand' (t:ts) info stack
|(head t) == '$' = (head (change t info )):expand' ts info stack
|otherwise = t: expand' ts info stack
where
change x y
= lookUp x (getKeywordDefs (snd (split ['\n'] y)))
Когда я делаю:
expand "Keywords (e.g. $x, $y, $z...) may appear anwhere, e.g. <$here>." "$x $a\n$y $b\n$z $c\n$here $this-is-one"
я получил
Exception: Prelude.head: empty list
но я хотел
"Keywords (e.g. $a, $b, $c...) may appear anwhere, e.g. <$this-is-one>."
Потому что я должен был
("Keywords (e.g. $x, $y, $z...) may appear anwhere, e.g. <$here>.",
"$x $a\n$y $b\n$z $c\n$here $this-is-one")
==> "Keywords (e.g. $a, $b, $c...) may appear anwhere, e.g. <$this-is-one>."
ДОПОЛНЕНИЕ Я определил разделить себя, то есть:
split :: [Char] -> String -> (String, [String])
split separators ( x : wordsrest)
| elem x separators = (x : listseparators,"" : word : listwords)
| otherwise = (listseparators, ((x : word) : listwords))
where
(listseparators, word : listwords) = split separators wordsrest
split _ _ = ("" , [""])
например, если я введу
split " .," "A comma, then some words."
тогда я получу
(" , .",["A","comma","","then","some","words",""])
1 ответ
Решение
Как вы можете видеть в документе, split
не хранит разделители в блоках, которые он выводит, поэтому они вполне могут быть пустыми.
Как следствие, имея (head t) == '$'
в охране небезопасно и может привести к возникновению исключений.