Iterating in Scheme
Let's say that I have a list of lists in Scheme, like so:
(define a-list (list (list a b) (list c d) (list e f) (list g h) (list i j)))
How would I go about def开发者_JS百科ining a function that would consume this list such that I could use another function to consume each letter in the list?
For instance, I had another function
(define (smorg a)
(...))
So, how do I go about doing smorg a, smorg b, smorg c... ?
What textbook are you using?
If the answer is How to Design Programs, then follow the design recipe. In particular, you need a data definition; the data definition is used to generate a template, which tells you exactly how to get the recursion right.
If you're not using HtDP, I highly suggest you give it a read. The book is available online, here: http://www.htdp.org/. Then follow the design recipe. In particular, you need a data definition etc etc etc.
In either case, here's a hint: Never, ever use the list
function before you've mastered recursion. Not in your functions, not even in your examples. It gets in your way. Build your lists with cons
and empty
.
Be sure you understand what list?
, map
, car
and cdr
do. They might be of use.
精彩评论