首页 > 解决方案 > 执行流程序时违反合同 - 方案

问题描述

我正在实现一个过程,它接受一个数字 n 和一个流,并返回一个新流,其中包含流中的 n 个第一个元素

(define (stream-take n stream)
    (cond ((stream-null? stream) 
           (the-empty-stream))
          ((= n 0) ('()))
          (else
           (cons (car stream)
               (stream-take (- n 1)
                 (force (cdr stream))
                 )))))

(stream-take 10 nats)

此代码给出错误消息

 =: contract violation
  expected: number?
  given: (2 . #<promise>)
  argument position: 1st
  other arguments...:

标签: streamscheme

解决方案


('()))

不是。它应该返回

(the-empty-stream)

反而。

PS:

  1. 另请注意,我已经编辑了您的原始帖子并在最后一个条款中添加了(else ...)。

  2. ('()))表示调用函数'(),这肯定不是函数。


推荐阅读