At Couples and Co. programmers create lists of couples from lists of symbols and numbers. The list starts with a symbol and every other of the following items on the list is a symbol, too; the remaining items on the list are numbers. You may assume that all given lists are of even length.
(define-struct couple (left right)) ;; A Couple is (make-couple Symbol Number)
Design the function create-couples.
Solution: Grader: Matthias F.
;; [PTS 2, for correct contract, which involves two data
;; defs and/or uses of [Listof ...]
;; create-couples :
;; [Listof (union Symbol Number)] -> [Listof Couple]
;; create couples from an even length list
;; generative recursion
(define (create-pairs l)
(cond
;; [PTS: 1 for the empty condition,
;; 1 for the ``generative'' recursions]
[(empty? l) empty]
[else (cons (make-couple (first l) (second l))
(create-pairs (rest (rest l))))]))
;; EXAMPLES/TESTS ;; [PTS 1, for examples/tests]
(equal? (create-pairs '())
empty)
(equal? (create-pairs '(a 10 b 20))
(list (make-couple 'a 10) (make-couple 'b 20)))