Lecture overview -- Keyboard shortcut: 'u'  Previous page: Example of recursion: <kbd>number-interval</kbd> -- Keyboard shortcut: 'p'  Next page: Examples with recursion: <kbd>string-of-char-list?</kbd> -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 3 - Page 22 : 42
Functional Programming in Scheme
Name binding, Recursion, Iteration, and Continuations
Examples of recursion: string-merge

The function string-merge zips two lists of strings to a single string. The lists are not necessarily of equal lengths

(define (string-merge str-list-1 str-list-2)
 (cond ((null? str-list-1) (apply string-append str-list-2))
       ((null? str-list-2) (apply string-append str-list-1))
       (else (string-append
              (car str-list-1) (car str-list-2)
              (string-merge (cdr str-list-1) (cdr str-list-2))))))

The recursive function string-merge. Notice that this function is a general recursive function. The recursive call, emphasized above, is not in a tail position, because of the embedding in string-append.

y:/Kurt/Files/scheme/lib/general.scmAn application of string-merge which converts a list of strings to a string with a given separator.

This is a typical task in a web program, where a list of elements needs to be aggregated for HTML presentation purposes. Notice the merging of a list of n elements with a list of length n-1. The function make-list is another LAML function; (make-list n el) makes a list of n occurrences of el.

y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/string-merge-iter.scmA tail recursive version of string-merge.