忽略范围内第一个功能的功能

时间:2018-10-09 20:07:54

标签: scheme

所以在我的函数中,我需要对范围a到b之间的所有数字求平方。

这是我的代码:

(define (square x)
  (* x x))

(define (consecutive-squares a b)
  (if (> a b)
      '()
      (map square (consecutive-ints(+ a 1)b))))

运行(consecutive-squares 1 10)的测试时,它输出(4 9 16 25 36 49 64 81 100),它完全丢失了1范围内的第一个数字。要通过示例测试,需要哪个

1 个答案:

答案 0 :(得分:0)

consecutive-ints函数知道如何将每个值加1以获得范围内的下一个值,调用时无需这样做。我也希望它能处理一个空范围,因此您不需要自己检查。

(define (consecutive-squares a b)
  (map square (consecutive-ints a b))
相关问题