球拍:采取:违反合同

时间:2016-05-04 18:04:40

标签: stream functional-programming lisp racket

我在linux上使用racket v6.5 repl并尝试从流教程https://docs.racket-lang.org/functional-data-structures/streams.html运行take函数示例。

但是,而不是预期的

> (take 3 (stream 1 2 3 4 5 6))
- : (Rec
 g1827317
 (U Null
    (Boxof (U (Pairof Integer g1827317) (-> (Pairof Integer g1827317))))))
'#&#<procedure:.../pfds/stream.rkt:41:7>

我得到了

> (take 3 (stream 1 2 3 4 5 6))
take: contract violation
expected: exact-nonnegative-integer?
given: #<stream>
argument position: 2nd
other arguments...:
 3
context...:
 /usr/share/racket/collects/racket/list.rkt:151:0: take
 /usr/share/racket/collects/racket/private/misc.rkt:87:7
> 

1 个答案:

答案 0 :(得分:2)

要使用该流库,您需要pfds/stream这样:

> (require typed/racket)
> (require pfds/stream)
> (take 3 (stream 1 2 3 4 5 6))
- : (Rec
     anonymous-module1715254
     (U Null
        (Boxof
         (U (Pairof Integer anonymous-module1715254)
            (-> (Pairof Integer anonymous-module1715254))))))
'#&#<procedure:.../pfds/stream.rkt:41:7>

完成后,takestream的正确版本将可用:

while(getline(openFile, line)){
    ++counter;
}