【发布时间】:2018-04-11 21:35:46
【问题描述】:
我写了一个程序,结果发现使用列表太慢了,所以我试图切换到序列。但是,在查看文档后,我似乎无法弄清楚正确的语法。
到目前为止,我正在尝试使用这个简单的代码来学习:
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
我已经玩了一段时间的语法,但没有运气,但它仍然有很多错误:
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
我是 Haskell 的新手,非常感谢任何帮助!
【问题讨论】:
-
你会如何对列表做同样的事情?
-
试图强制 Haskell 程序进入其他语言的“语句序列”模型通常不会导致 A Good Place。
-
您上次的编辑完全改变了问题,请不要这样做。
标签: haskell