I’m trying to translate some exercises from The Little Schemer into Clojure for my students and I’m not sure about the idiomatic way to do things. Here’s the function I’ve got. It seems to work. I know this isn’t the way you’re supposed to do it. I’m just trying to introduce them to some basic ideas about recursion.
(head (title "the fortune cookie institute"))
(h1 "Welcome to the Fortune Cookie Institute")
(p "Our fortunes are guaranteed accurate no matter what.")
(p "Like these")
(li "You will gain weight")
(li "Taxes will rise")
(li "Fusion power will always be 50 years away"))
(p "Submit your own fortunes to email@example.com!")))
My question is this. I know this is inadequate for a number of reasons. What’s the right way to write a recursive function on a nested set of collections in Clojure?
If you want to cheat, you can look at the source for clojure.walk/prewalk and clojure.walk/postwalk
I think what you’ve written is perfectly fine. In contrast to Scheme, clojure lacks automatic tail call optimization. So your implementation might blow the stack for big trees. To avoid that, look into recur.
You also might want to work with sequences, not lists. That way, you can support vectors.
Edit: looks like clojure.walk simply uses map and reduce.