Skip to content

Commit 176b023

Browse files
committed
Week11-Accumulators
Week11 - Context-Preserving, Result-so-far and Worklist Accumulators. Optimizing Recursion into Tail-Recursion using accumulators.
1 parent dfa009b commit 176b023

File tree

10 files changed

+43644
-0
lines changed

10 files changed

+43644
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
;; The first three lines of this file were inserted by DrRacket. They record metadata
2+
;; about the language level of this file in a form that our tools can easily process.
3+
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname Practice01_String-concat-with-position) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
4+
;; You are asked to design a function that numbers a list of strings by inserting a number
5+
;; and a colon before each element of the list based on its position.
6+
7+
8+
9+
;; (listof String) -> (listof String)
10+
;; append each string's position in the list to the front of the string to number the list
11+
12+
(check-expect (number-list empty) empty)
13+
(check-expect (number-list (list "first" "second" "third"))
14+
(list "1: first" "2: second" "3: third"))
15+
16+
;; (define (number-list los) los) ;stub
17+
18+
(define (number-list lon0)
19+
;acc: 1-based position of the (first lon) of lon0;
20+
;(number-list (list "first" "second" "third") 1)
21+
;(number-list (list "second" "third") 2)
22+
;(number-list (list "third") 3)
23+
(local [(define (number-list lon acc)
24+
(cond [(empty? lon) empty]
25+
[else
26+
(cons (string-append (number->string acc) ": " (first lon))
27+
(number-list (rest lon) (add1 acc)))]))]
28+
(number-list lon0 1)))

0 commit comments

Comments
 (0)