10 POINTS |
Problem 5. Your manager just came home from a consulting visit with a client. This (new) client keeps around non-empty lists of strings. Your company sold this client a ``sentence packaging'' program, which concatenates a list of strings into a single string. The original strings are separated via commas in the ``packaged string.'' Develop the program.
;; A Los is one of: ;; -- (cons String empty) ;; -- (cons String Los)
Solution:
;; pack : Los -> String [PT 1] ;; concatenate the strings on alos; ;; separate them via commas [PT 1] (define (pack alos) (cond ;; [PT 3, two for cond lines, ;; one for (empty? (rest ...))] [(empty? (rest alos)) (first alos)] ;; [PT 3, one for correctness, ;; two for "template" expressions ] [else (string-append (first alos) "," (pack (rest alos)))])) ;; Examples/Tests: [PT 2, for correct tests/examples] (string=? (pack (cons "hello" (cons "world" empty))) "hello,world") (string=? (pack (cons "friedman" (cons "dan" empty))) "friedman,dan")