0

以下代码无法编译。我收到类型错误。我认为这将是更好的版本,因为它清楚地将两种不同的情况分开......该函数应该有助于确定有限状态机是否接受输入字。

import Text.Show.Functions
import qualified Data.Set as Set
import qualified Data.List as List

setTransition :: (Int -> Char -> [Int])  -> [Int] -> Char -> [Int]
setTransition delta [] sigma            = []
setTransition delta xs@[x:xs'] sigma    = foldl f [] xs
                where f ys q = (delta q sigma) `List.union` ys

但是,这(删除了模式匹配)确实可以编译。有人能告诉我为什么吗??

import Text.Show.Functions
import qualified Data.Set as Set
import qualified Data.List as List

setTransition :: (Int -> Char -> [Int])  -> [Int] -> Char -> [Int]
setTransition delta [] sigma            = []
setTransition delta xs sigma    = foldl f [] xs
                where f ys q = (delta q sigma) `List.union` ys

谢谢帮助

4

1 回答 1

6

您刚刚遇到语法错误:

xs@[x:xs']

应该

xs@(x:xs')

但你实际上并不需要两种情况,因为

foldl f b [] = b

所以

foldl f [] [] = []

就像您的空列表案例需要的那样。

事实上,在[]没有模式匹配和(x:xs)使用的情况下进行模式匹配是非常罕见的——这就是我知道如何考虑在这两种情况下是否会给你正确的答案。不过,它确实偶尔会发生。xxsfoldl

于 2013-02-21T18:21:29.467 回答