Posted By

magicrebirth on 03/19/11


Tagged

python recursion scheme


Versions (?)

recursion in scheme and python


 / Published in: Python
 

URL: http://stackoverflow.com/questions/5300242/scheme-to-python-most-elegant-translation-of-a-recursive-procedure

  1. # scheme:
  2.  
  3. (define (downup wd)
  4. (if (= (count wd) 1)
  5. (se wd)
  6. (se wd (downup (bl wd)) wd)))
  7.  
  8. > (downup 'toe)
  9. (TOE TO T TO TOE)
  10.  
  11. > (downup 'banana)
  12. (BANANA BANAN BANA BAN BA B BA BAN BANA BANAN BANANA)
  13.  
  14.  
  15. # now in python
  16.  
  17. def downup(word):
  18. if len(word) == 1:
  19. return [word]
  20. return [word] + downup(word[1:]) + [word]
  21.  
  22. print downup("bananas")

Report this snippet  

You need to login to post a comment.