我如何调用一个集成在Haskell类型中的函数? [英] How Can I call a function, that is integrated in a type in Haskell?

查看:136
本文介绍了我如何调用一个集成在Haskell类型中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学生,在编程课程中,我们必须学习Haskell。所以我是新手,并没有那么多的经验。我也不熟悉在论坛中发布问题。

I am student and in my programming course we have to learn Haskell. So I am new to it and i don't have that much experience. Also I am not familiar with posting questions in a forum.

因此,首先我会发布图书馆,我必须与之合作。
(DA:确定性自动机)

So first of all I will post the library, I have to work with. (DA : Deterministic Automaton)

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函数在其列表中显示一个自动机,并将其转换为自动机。这个函数和库的其余部分由讲座的主席给出。

The toDA function takes an automaton in its list representation and converts it into an automaton. This function and the rest of the library is given by the chair of the lecture.

现在的问题是编写一个类型为
的函数

The problem is now to write a function of type

advance :: DA -> State -> String -> State

该函数接受一个自动机,一个状态和一个字符串,并在读取之后返回自动机的状态字符串。

This function takes an automaton, a state and a String and returns the state of the automaton after reading the String.

到目前为止,这个想法很清楚。 DA型自动机具有状态转换函数增量。所以函数advance必须以某种方式调用delta函数。但是,如何访问一个集成在类型中的函数?

The Idea is clear so far. An automaton of type DA has got a state-transition-function delta. So the function "advance" has to call that delta function in some way. But how can I access a function, that is integrated in a type?

推荐答案

您使用模式匹配:

advance :: DA -> State -> String -> State
advance (start, step, accept) fromState chars = ....

类型关键字只是介绍类型同义词 DA 只是三元的一个同义词(整数,整数 - >字符 - >整数,整数 - >布尔)

The type keyword just introduces type synonyms. DA is just a synonym for a triple (Integer, Integer -> Char -> Integer, Integer -> Bool).

你的命名很混乱。在 a 自动机的定义中 delta 是一个状态转换函数,但是在 toDA 函数,名为 delta 的参数是一个列表。 ListDA 类型也是三元组的一个同义词(一个不同的 - 一个状态,一个转换列表和一个可接受状态列表)。

Your naming is confusing. delta in the definition of a automaton is a state transition function, but in the definition of toDA function, a parameter named delta is a list. ListDA type is also just a synonym for a triple (a different one - of a state, a list of transitions, and a list of acceptable states).

下面是如何使用循环递归来编写代码:

Here is how this can be coded, using recursion for loops:

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 接受变量,所以我们可以使用匿名变量模式 _ 那里。此外, step 是类型 State - >的一个函数。字符 - >状态,并且规定了函数调用中使用的参数的顺序。即它接受一个状态和一个字符,并产生一个新的状态。

Notice we have no need here for the start or accept variables, so we can use the anonymous variable pattern _ there. Also, step is a function of type State -> Char -> State, and that dictates the order of arguments used in the function call there. I.e. it accepts a state and a character, and produces a new state.

如果你根本不了解Haskell,你可能会从阅读(并通过)一个很好的教程,像这个

If you don't know Haskell at all, you will likely benefit from reading (and working through) a good tutorial, like this one.

最后,既然你说过你不熟悉在论坛上发布问题,请阅读接受答案 ,以及一般常见问题

Lastly, since you've said you're "not familiar with posting questions in a forum", please read about accepting answers, and FAQ in general.

这篇关于我如何调用一个集成在Haskell类型中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆