Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4054936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:38:18+00:00 2026-05-20T14:38:18+00:00

I recent read again the beautiful intro to recursion in the ‘simply scheme’ book

  • 0

I recent read again the beautiful intro to recursion in the ‘simply scheme’ book (here’s the link to the relevant chapter), where this recursive procedure is introduced (in the scheme language):

(define (downup wd)
  (if (= (count wd) 1)
      (se wd)
      (se wd (downup (bl wd)) wd)))

> (downup 'toe)
(TOE TO T TO TOE)

> (downup 'banana)
(BANANA BANAN BANA BAN BA B BA BAN BANA BANAN BANANA)

I tried to translate that procedure into python, which I use in my day job. Here’s the result:

def recursivefun(word):
    if len(word) == 1: 
        return word
    else:
        x = []
        x.append(word)
        x.extend(recursivefun(word[1:]))
        x.append(word)
        return x

print recursivefun("ciao")
# ==> ['ciao', 'iao', 'ao', 'o', 'ao', 'iao', 'ciao']

So my question is: is there a better way to represent this recursive procedure in python? Or maybe a more ‘elegant’ way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:38:19+00:00Added an answer on May 20, 2026 at 2:38 pm

    If you want to closely represent the original recursive Scheme function:

    def downup(word):
        if len(word) <= 1:
            return [word]
        return [word] + downup(s[1:]) + [word]
    

    Note that your own function returns a string if the length of the passed in string is 1 and a list otherwise. This could lead to surprising behaviour. Try

    def recursivefun(word):
        if len(word) == 2:
            return word
        else:
            x = []
            x.append(word)
            x.extend(recursivefun(word[1:]))
            x.append(word)
            return x
    
    print recursivefun("banana")
    

    for example, which prints

    ['banana', 'anana', 'nana', 'ana', 'n', 'a', 'ana', 'nana', 'anana', 'banana']
    

    which might be different from what you expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm profiling a recent program that is dominated with File read. I'm kind of
I am unable to find any recent example on how to read EXIF/IPTC information
As part of a recent project I had to read and write from a
I've read Cakes section on Apache but it doesn't cover my question. Here's my
Having just read the recent article in Wired , I'm curious: what is it
I'm using the most recent PI-OLEDB library to read data from aggregate views in
I read that oracle's CBO(on recent versions) is so good that even if worst
any idea where I can read logs of recent queries? Errors are coming back
I've read a lot about openid in recent few days and found out it's
Has been studying Tvisted week, read the book and most of the docks, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.