This commit is contained in:
NaiJi ✨ 2021-05-02 20:25:25 +03:00
parent 43cb37c8ea
commit bc59be89e5
1 changed files with 35 additions and 0 deletions

35
sicp-ex-1.32.rkt Normal file
View File

@ -0,0 +1,35 @@
#! /usr/bin/racket
#lang racket/base
(define (accumulate-iter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner (term a) result))))
(iter a null-value))
(define (product-iter term a next b)
(accumulate-iter * 1 term a next b))
(define (sum-iter term a next b)
(accumulate-iter + 0 term a next b))
(sum-iter (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
(product-iter (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (accumulate-recur combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate-recur combiner null-value term (next a) next b))))
(define (product-recur term a next b)
(accumulate-recur * 1 term a next b))
(define (sum-recur term a next b)
(accumulate-recur + 0 term a next b))
(sum-recur (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)
(product-recur (lambda (x) x) 1 (lambda (x) (+ x 1)) 4)