-1

这是我对扩展功能的尝试:

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",""])
4

1 回答 1

1

正如您在文档中看到的那样,split它不会将分隔符保留在它输出的块中,因此它们很可能是空的。

因此,拥有(head t) == '$'守卫是不安全的,并且可能导致引发异常。

于 2015-01-03T00:54:05.047 回答