Here's my attempt at an expand function:
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)))
When I do:
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"
I get
Exception: Prelude.head: empty list
but I wanted
"Keywords (e.g. $a, $b, $c...) may appear anwhere, e.g. <$this-is-one>."
Because I should have
("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>."
ADDITION i defined split myself ,that is:
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 _ _ = ("" , [""])
for example if I input
split " .," "A comma, then some words."
then i'll get
(" , .",["A","comma","","then","some","words",""])
As you can see in the doc, split
does not keep the separators in the chunks it outputs so they may very well be empty.
As a consequence, having (head t) == '$'
in a guard is unsafe and can lead to exceptions being raised.