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 6820291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:24:26+00:00 2026-05-26T21:24:26+00:00

Here are some snippets in different languages. Function double in question is taken from

  • 0

Here are some snippets in different languages.

Function double in question is taken from SICP, ex. 1.41.

Lisp:

(define (double f) (lambda (x) (f (f x))))
(define (inc x) (+ x 1))
(((double (double double)) inc) 5)

Python:

def double(f):
  def result(x):
    return f(f(x))
  return result

def inc(x):
  return x + 1

double(double(double(inc)))(5)

Javascript:

var double = function(f) {
  return function(x) { return f(f(x)) };
};

var inc = function(x) { return x + 1  };

(double(double(double(inc))))(5);

Ruby:

double = lambda {|f| lambda {|x| f[f[x]] } }
inc = lambda {|x| x+1 }
double[double[double[inc]]][5]

If I am not insane, these functions should do the same thing and return the same result.
However, lisp version returns 21 whereas others return 13. Can you explain that difference to me? Am I missing something?

  • 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-26T21:24:27+00:00Added an answer on May 26, 2026 at 9:24 pm

    How you call the functions in the scheme code is different from the others. The equivalent python would be:

    double(double(double))(inc)(5)
    

    In words, the scheme code creates a function that applies another function 16 times, and applies that function to inc. The python creates functions that apply inc 8 times; the others work the same as the python.

    The difference might be a bit clearer if you introduce names for the intermediate steps. In scheme:

    (define quadruple (double double))
    (define hexadecuple (double quadruple)) ; hexadecuple may not actually be a word... 
    (define add16 (hexadecuple inc))
    (add16 5)
    

    I hope that’s correct syntax; it’s been a while since I’ve done anything with scheme.

    In python:

    add2 = double(inc)
    add4 = double(add2)
    add8 = double(add4)
    add8(5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use UDP Sokckts in my client application. Here are some code snippets: SendIP
Here's some background on what I'm trying to do: Open a serial port from
here is some sample javascript: SomeObjectType = function() { } SomeObjectType.prototype = { field1:
I'm trying to apply CSS styles to some HTML snippets that were generated from
Here's an interesting puzzle. I downloaded Snippet Compiler to try some stuff out, and
Here is some code I could not get to format properly in markdown, this
Here is some simple Perl to count the number of times a value occurs
Here's some Ruby code: puts %x{ pstree #{$$} } # never forks puts %x{
Here's some code I saw once. Can you see what's wrong with it? [updated]
Here is some XAML <HierarchicalDataTemplate DataType={x:Type data:FolderEntity} ItemsSource={Binding Path=FolderEntities,UpdateSourceTrigger=PropertyChanged}> <Label Content={Binding FolderName}/> </HierarchicalDataTemplate> <TreeView/>

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.