Haskell - 从自定义类型返回项目 [英] Haskell - Return item from custom type

查看:129
本文介绍了Haskell - 从自定义类型返回项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个类的Haskell问题,我似乎无法获得将某个项目拉出自定义类型的语法。这里是类型:

pre $ - 有限状态机M =(Q,q0,F,d)

type FSM =([Int],Int,[Int],[(Int,Char,Int)])

以下是我一直在使用的测试值:

  testFSM :: FSM 
$ (1,'b',2),(2,'b',3),(1,'b',1),b $ b testFSM =([1,2,3] ])

我希望能够从FSM类型中提取每条数据,但是我我不太确定我会怎么做。我试过这个:

  fsmGetQ fsm = [q | (q,q0,f,d)< -  fsm] 

我得到的testFSM:

 < interactive>:102:9:
无法匹配类型`([ (t0,t10,t20,t30)]'
''Int',Int,[Int],[(Int,Char,Int)])'
期待的类型:[(t0,t10 ,t20,t30)]
实际类型:FSM
在`fsmGetQ'的第一个参数中,即`testFSM'
在表达式中:fsmGetQ testFSM
在`它':it = fsmGetQ testFSM

我有很多辅助函数为单个元素,但我不知道如何从FSM类型中提取它们。

解决方案

p>

  fsmGetQ(q,_,_,_)= q 


I'm working on a Haskell problem for a class, and I can't seem to get the syntax right for pulling an item out of a custom type. Here's the type:

-- Finite State Machine M = (Q, q0, F, d)

type FSM = ([Int], Int, [Int], [(Int,Char,Int)])

And here's a test value I've been working with:

testFSM :: FSM

testFSM = ( [ 1, 2, 3 ], 1, [ 3 ], [ ( 1, 'a', 2 ), ( 2, 'b', 3 ), ( 1, 'b', 1 ) ] )

I want to be able to pull out each piece of data from the FSM type, but I'm not quite sure on how I would do that. I have tried this:

fsmGetQ fsm = [ q | ( q, q0, f, d ) <- fsm ]

but if I run that function with the testFSM I get:

<interactive>:102:9:
    Couldn't match type `([Int], Int, [Int], [(Int, Char, Int)])'
                  with `[(t0, t10, t20, t30)]'
    Expected type: [(t0, t10, t20, t30)]
      Actual type: FSM
    In the first argument of `fsmGetQ', namely `testFSM'
    In the expression: fsmGetQ testFSM
    In an equation for `it': it = fsmGetQ testFSM

I've got a lot of helper functions working for the individual elements, but I can't figure out how to extract them from the type FSM.

解决方案

You can extract like this:

fsmGetQ (q, _, _, _) = q

这篇关于Haskell - 从自定义类型返回项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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