recursion in scheme and python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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")

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.