我是学生,在我的编程类(class)中我们必须学习 Haskell。所以我是新手,我没有那么多经验。此外,我不熟悉在论坛中发布问题。
所以首先我会发布图书馆,我必须与之合作。 (DA:确定性自动机)
type State = Integer
type DA = (State, State -> Char -> State, State -> Bool)
type ListDA = (State, [((State, Char), State)], [State])
a :: DA
a = (0, delta, (==1))
where
delta 0 'a' = 1
delta 1 'a' = 1
delta 2 'a' = 1
delta 0 'b' = 2
delta 1 'b' = 2
delta 2 'b' = 2
toDA :: ListDA -> DA
toDA (start, delta, final) = (start, deltaFun delta, (`elem` final))
where deltaFun dl = curry (fromMaybe 0 . flip lookup dl)
toDA 函数在其列表表示中采用自动机并将其转换为自动机。此函数和库的其余部分由讲座主席提供。
现在的问题是写一个类型的函数
advance :: DA -> State -> String -> State
此函数接受一个自动机、一个状态和一个字符串,并在读取字符串后返回自动机的状态。
到目前为止,这个想法很清楚。 DA 类型的自动机具有状态转移函数增量。所以函数“advance”必须以某种方式调用该增量函数。但是我怎样才能访问一个集成在类型中的函数呢?
最佳答案
为此使用模式匹配:
advance :: DA -> State -> String -> State
advance (start, step, accept) fromState chars = ....
type
关键字只是引入了 type synonyms . DA
只是三元组 (Integer, Integer -> Char -> Integer, Integer -> Bool)
的同义词。
您的命名令人困惑。 a
自动机定义中的delta
是一个状态转换函数,但是在toDA
函数的定义中,一个名为delta<的参数
是一个列表。 ListDA
类型也只是三元组的同义词(不同的三元组 - 状态、转换列表和可接受状态列表)。
这是如何使用递归循环来编码的:
advance (_, step, _) fromState chars = go fromState chars
where
go s [] = ... -- stop, and produce the state as the answer,
-- when the input string (list of chars) has ended
go s (c:cs) = -- if not, then
let s2 = step s c -- make one step
in go ....... -- and loop with new values
注意我们这里不需要start
或accept
变量,所以我们可以在那里使用匿名变量模式_
。此外,step
是 State -> Char -> State
类型的函数,它规定了函数调用中使用的参数顺序。 IE。它接受一个状态和一个字符,并产生一个新的状态。
如果您根本不了解 Haskell,您可能会从阅读(并完成)一个很好的教程中受益,like this one .
最后,既然您说您“不熟悉在论坛中发布问题”,请阅读 accepting answers , 和 FAQ一般而言。
关于haskell - 如何调用集成在 Haskell 类型中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13472606/