异常:Prelude.head:空列表

Exception: Prelude.head: empty list

这是我对扩展函数的尝试:

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) == '

当我这样做时:

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,即:

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",""])
= (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,即:

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

如您所见,in the docsplit 没有将分隔符保留在它输出的块中,因此它们很可能是空的。

因此,将 (head t) == '$' 放在守卫中是不安全的,并可能导致引发异常。