简体   繁体   English

在Haskell中使用Data.Sequence

[英]Working with Data.Sequence in Haskell

I wrote a program that turned out to be far too slow using lists so I'm trying to switch over to sequences. 我写了一个程序,发现使用列表太慢了,所以我试图切换到序列。 However, I can't seem to figure out the correct syntax after looking at the documentation. 但是,在查看文档后,我似乎无法找出正确的语法。

So far I'm trying to learn with this simple code: 到目前为止,我正在尝试学习以下简单代码:

import Control.Monad
import qualified Data.Sequence as S

main :: IO ()
main = do 
       let testSeq = S.empty
       testSeq S.|> 5
       testSeq S.|> 20
       testSeq S.|> 3
       let newSeq = S.update 2 3 testSeq
       let x = lookup 2 testSeq
       print x

I've played around with the syntax for a while with no luck but it still has a ton of errors: 我已经尝试了一段时间的语法,但运气不佳,但是仍然有很多错误:

test.hs:9:8:
    Couldn't match expected type ‘IO a0’
                with actual type ‘S.Seq Integer’
    In a stmt of a 'do' block: testSeq S.|> 5
    In the expression:
      do { let testSeq = S.empty;
           testSeq S.|> 5;
           testSeq S.|> 20;
           testSeq S.|> 3;
           .... }
    In an equation for ‘main’:
        main
          = do { let testSeq = ...;
                 testSeq S.|> 5;
                 testSeq S.|> 20;
                 .... }

test.hs:10:8:
    Couldn't match expected type ‘IO a1’
                with actual type ‘S.Seq Integer’
    In a stmt of a 'do' block: testSeq S.|> 20
    In the expression:
      do { let testSeq = S.empty;
           testSeq S.|> 5;
           testSeq S.|> 20;
           testSeq S.|> 3;
           .... }
    In an equation for ‘main’:
        main
          = do { let testSeq = ...;
                 testSeq S.|> 5;
                 testSeq S.|> 20;
                 .... }

test.hs:11:8:
    Couldn't match expected type ‘IO a2’
                with actual type ‘S.Seq Integer’
    In a stmt of a 'do' block: testSeq S.|> 3
    In the expression:
      do { let testSeq = S.empty;
           testSeq S.|> 5;
           testSeq S.|> 20;
           testSeq S.|> 3;
           .... }
    In an equation for ‘main’:
        main
          = do { let testSeq = ...;
                 testSeq S.|> 5;
                 testSeq S.|> 20;
                 .... }

test.hs:13:25:
    Couldn't match expected type ‘[(Integer, b)]’
                with actual type ‘S.Seq a3’
    Relevant bindings include x :: Maybe b (bound at test.hs:13:12)
    In the second argument of ‘lookup’, namely ‘testSeq’
    In the expression: lookup 2 testSeq

I'm new to Haskell so any help would be greatly appreciated! 我是Haskell的新手,所以我们将不胜感激!

Like almost everything in Haskell, Seq is a purely functional data structure. 就像Haskell中的几乎所有内容一样, Seq是纯功能数据结构。 It's not like an imperative stack where you push stuff to, mutating the original structure. 这不像您将内容推送到其中以使原始结构发生变化的命令式堆栈。 Rather, like ordinary lists, you just generate new container-values which have the extra elements, but this does not affect the old shorter seqs in any way. 相反,像普通列表一样,您只是生成具有额外元素的新容器值,但这不会以任何方式影响旧的较短序列。 So, the program you've asked about should just be 因此,您所要求的程序应该是

testSeq :: S.Seq Int
testSeq = S.update 2 3 $ S.empty S.|> 5 S.|> 20 S.|> 30

main :: IO ()
main = print $ S.lookup 2 testSeq

(It has to be S.lookup , or equivalently S.!? . By itself, lookup is a function that works on plain lists!) (它必须是S.lookup或等效的S.!? 。就其本身而言, lookup是在普通列表上起作用的函数!)

Note that this doesn't really give you any advantage over 请注意,这实际上并没有给您带来任何优势

testSeq = S.update 2 3 $ S.fromList [5,20,30]

In fact, lists are usually faster than Seq , they just don't allow efficient random access. 实际上,列表通常比Seq ,它们只是不允许有效的随机访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM