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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:14:59+00:00 2026-06-19T02:14:59+00:00

I am having trouble with some tricky-looking lambda expressions in Scheme, and I would

  • 0

I am having trouble with some tricky-looking lambda expressions in Scheme, and I would like to see how they are being evaluated by the interpreter.

I would like the Scheme interpreter to print all the evaluation steps, as seen in SICP Section 1.1.5, “The Substitution Model for Procedure Application”.

I am looking for a solution using any Scheme interpreter. I have already tried Racket’s tracing, but it only traces procedure calls, not every expression.

Motivating example

Given the definition of Church numerals from SICP Exercise 2.6:

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

and the task:

Define one and two directly (not in terms of zero and add-1).

I wish to check my definitions of one and two against the results of evaluating (add-1 zero) and (add-1 (add-1 zero)).

This is what I would like the Scheme interpreter to print out:

> (add-1 zero)
(add-1 (lambda (f) (lambda (x) x)))
(lambda (f) (lambda (x) (f (((lambda (f) (lambda (x) x)) f) x))))
(lambda (f) (lambda (x) (f ((lambda (x) x) x))))
(lambda (f) (lambda (x) (f x)))
>
  • 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-06-19T02:15:00+00:00Added an answer on June 19, 2026 at 2:15 am

    This is very easy with combinators-like equations (what was once called applicative style I believe )

    zero f x = x
    add1 n f x = f (n f x)
    
    one f x = add1 zero f x = f (zero f x) = f x         **(1)**
    two f x = add1 one f x = f (one f x) = f (f x)       **(2)**
    

    With combinators, everything is curried: a b c d is actually (((a b) c) d) and a b c = d is equivalent to (define a (lambda (b) (lambda (c) d))).

    Now it is clear what is the intended meaning of f and x: x stands for a concrete implementation of “zero” data element, and f stands for a concrete implementation of “successor” operation, compatible with a given concrete implementation of “zero”. f and x should have really be named mnemonically:

    zero s z = z
    add1 n s z = s (n s z)
    

    Not so tricky-looking anymore, with more convenient syntax, right? lambda itself was a typographical accident anyway. Now,

    one s z = s z         ; e.g. (1+ 0)
    two s z = s (s z)     ; e.g. (1+ (1+ 0))
    

    Tracing the steps according to the SICP 1.1.3 combinations evaluation procedure,

    • To evaluate a combination, do the following:
      1. Evaluate the subexpressions of the combination.
      2. Apply the procedure that is the value of the leftmost subexpression (the operator) to the arguments that are the values of the other subexpressions (the operands).

    and the 1.1.5 sustitution model for procedure application

    • To apply a compound procedure to arguments, evaluate the body of the procedure with each formal parameter replaced by the corresponding argument.

    we get

    add1 zero = 
      ( n f x => f (n f x) ) ( f x => x ) =
      ( f x => f ( ( f x => x ) f x ) )
    

    and here the substitution stops actually, because the result is a simple lambda expression, i.e. not a combination. Only when two more arguments are supplied, the evaluation is done in full:

    add1 zero s z = 
      ( n f x => f (n f x) ) ( f x => x ) s z =
      ( f x => f ( ( f x => x ) f x ) ) s z =
      ( x => {s} ( ( f x => x ) {s} x ) ) z =    ; {s} is definition-of s
      {s} ( ( f x => x ) {s} {z} ) =             ; {z} is definition-of z
      ; must find the value of the operand in combination
      {s} ( ( x => x ) {z} ) = 
      {s} {z}
    

    and then the calculation will proceed according to the actual definitions of s and z. That is what the equations (1) shown above indicate, in shorter notation.

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

Sidebar

Related Questions

I'm having trouble some special characters in Qt, the 2 being the quote and
I'm having some trouble with a tricky SQL-query. In my MySQL database there is
I am having trouble with some javascript. The code snippet below creates a simple
I am having trouble with some of my rubygems, in particular those that use
I'm having trouble with some Excel data validation. On one sheet, I have a
I'm having trouble with some Regex code while scraping YouTube playlist pages. It mostly
I'm having trouble with some data I've imported from an XML file. Unfortunately I'm
I'm having trouble getting some javascript and Jquery to delay the appropriate amount of
I am building a site for experiment and I am having trouble with some
I'm pretty new to JavaScript and I'm having trouble with some of the properties

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.