Higher-Order String Functions
January 26, 2016
Scheme provides the higher-order functions map
, for-each
and fold
that operate on lists. In today’s exercise we will write versions of those functions that operate on strings:
(string-map proc str)
appliesproc
to each character instr
, replacing the character with the output ofproc
.proc
takes a single character and returns a single character.
(string-for-each proc str)
appliesproc
to each character instr
; the function is executed only for its side-effects.proc
takes a single character and returns nothing.
(string-fold proc base str)
calls function(proc base c)
on each character ofstr
, working left to right, accumulating the result inbase
as it goes.proc
takes abase
of any type, and a character, and returns a value of the same type asbase
.(string-fold-right proc base str)
is the same, except that it works from right to left.
Your task is to implement these four functions in the language of your choice. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.