I've written a map and a flatmap function, and I'm trying to figure out the best way to see how the flatmap function works as a standalone. Here are my two definitions:
(define (map function lst)
(if (null? lst)
; if we have reached the end of the list, return an empty list
; this essentially terminates the list that we are adding below with the cons
nil
; we do a cons -- with the scalar produced by the function call on the current element
; and the list proceduced by passing the rest of the elements again to the map function
(cons (function (car lst)) (map function (cdr lst)))))
(define (flatmap function lst)
(if (null? lst)
nil
; really the only difference here is we do an append instead of a cons
(append (function (car lst)) (flatmap function (cdr lst)))))
And to call map is quite easy, I just pass it a function that accepts a single argument (list element):
(map (lambda (x) (* x x)) '(1 2 3 4 5))
; (1 4 9 16 25)
However, I'm having a hard time coming up with a sample input/function that I can pass directly to the flatmap function to see how it works. What would be an example or two that would demonstrate how it works directly?