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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:45:44+00:00 2026-06-06T14:45:44+00:00

I’m trying to solve this problem in a pure-functional way, without using set! .

  • 0

I’m trying to solve this problem in a pure-functional way, without using set!.

I’ve written a function that calls a given lambda for each number in the fibonacci series, forever.

(define (each-fib fn)
  (letrec
    ((next (lambda (a b)
             (fn a)
             (next b (+ a b)))))
    (next 0 1)))

I think this is as succinct as it can be, but if I can shorten this, please enlighten me 🙂

With a definition like the above, is it possible to write another function that takes the first n numbers from the fibonacci series and gives me a list back, but without using variable mutation to track the state (which I understand is not really functional).

The function signature doesn’t need to be the same as the following… any approach that will utilize each-fib without using set! is fine.

(take-n-fibs 7) ; (0 1 1 2 3 5 8)

I’m guessing there’s some sort of continuations + currying trick I can use, but I keep coming back to wanting to use set!, which is what I’m trying to avoid (purely for learning purposes/shifting my thinking to purely functional).

  • 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-06T14:45:45+00:00Added an answer on June 6, 2026 at 2:45 pm

    Try this, implemented using lazy code by means of delayed evaluation:

    (define (each-fib fn)
      (letrec
          ((next (lambda (a b)
                   (fn a)
                   (delay (next b (+ a b))))))
        (next 0 1)))
    
    (define (take-n-fibs n fn)
      (let loop ((i n)
                 (promise (each-fib fn)))
        (when (positive? i)
          (loop (sub1 i) (force promise)))))
    

    As has been mentioned, each-fib can be further simplified by using a named let:

    (define (each-fib fn)
      (let next ((a 0) (b 1))
        (fn a)
        (delay (next b (+ a b)))))
    

    Either way, it was necessary to modify each-fib a little for using the delay primitive, which creates a promise:

    A promise encapsulates an expression to be evaluated on demand via force. After a promise has been forced, every later force of the promise produces the same result.

    I can’t think of a way to stop the original (unmodified) procedure from iterating indefinitely. But with the above change in place, take-n-fibs can keep forcing the lazy evaluation of as many values as needed, and no more.

    Also, take-n-fibs now receives a function for printing or processing each value in turn, use it like this:

    (take-n-fibs 10 (lambda (n) (printf "~a " n)))
    > 0 1 1 2 3 5 8 13 21 34 55
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.