Skip to content

Instantly share code, notes, and snippets.

@ultrox
Created January 10, 2021 00:12
Show Gist options
  • Save ultrox/f699ba5502cbaaa262023e70d04d3ef0 to your computer and use it in GitHub Desktop.
Save ultrox/f699ba5502cbaaa262023e70d04d3ef0 to your computer and use it in GitHub Desktop.
;; PROBLEM:
;; Create function that satisfiy following example
(check-expect (number-list (list "first" "second" "third"))
(list "1: first" "2: second" "3: third"))
(define (number-list n) empty) ; stub
;; #1 Objective:
;; - Satisfy all design elements (or as much as posible)
;; (listof String) -> (listof String)
;; append each string's position in the list to the front of the string to number the list
(check-expect (number-list empty) empty)
(check-expect (number-list (list "first" "second" "third"))
(list "1: first" "2: second" "3: third"))
;(define (number-list los) los) ;stub
#;
(define (number-list lon0)
;; acc: Natural; 1-based position of (first lon) in lon
;; (number-list (list "first" "second" "third") 1)
;; (number-list (list "second" "third") 2)
;; (number-list (list "third") 3)
(local [(define (number-list lon acc)
(cond [(empty? lon) empty]
[else
(... acc
(first lon)
(number-list (rest lon)
(add1 acc)))]))]
(number-list lon0 1)))
(define (number-list lon0)
;; acc: Natural; 1-based position of (first lon) in lon
;; (number-list (list "first" "second" "third") 1)
;; (number-list (list "second" "third") 2)
;; (number-list (list "third") 3)
(local [(define (number-list lon acc)
(cond [(empty? lon) empty]
[else
(cons (string-append (number->string acc) ": " (first lon))
(number-list (rest lon) (add1 acc)))]))]
(number-list lon0 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment