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

  • Home
  • SEARCH
  • 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 8422063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:19:54+00:00 2026-06-10T03:19:54+00:00

Recursion is cool, but sort of low level when you are surrounding by higher

  • 0

Recursion is cool, but sort of low level when you are surrounding by higher order library functions. I am trying to avoid writing a recursive function for a process that depends on the last value generated.

I would usually use iterate function in Clojure over a “zipped” list of last value and current parameter. Is there an equivalent function in Scala’s collection API?

Here is an attempt at an abstract example in some crazy pseudo code:

Say you have

  • An input list: Seq(1,2,3)
  • Some action you perform to the last value generated and the next item in the list:

    lastValue ^ 2 + nextInt(i)

and you want to accumulate all the values generated.

I am trying avoid writing something similar to:

def f(ls:Seq[Int]):Seq[Float] = {

  def g(pos:Int, lastGen:Float):Seq[Float] = { 
    val v = gen(lastGen, ls(pos))
    if( end(v) )
      Seq(v)
    else
      Seq(v) ++ g(pos+1, v)
  }

  f(0, 1)
}

I have seen something similar in defining a lazy stream version of Fibonacci in Haskell, so hypothetically I could use a lazy stream that referred to itself, but that is harder to wrap my brain around than Clojure’s iterate.

  • 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-10T03:19:56+00:00Added an answer on June 10, 2026 at 3:19 am

    It sounds like the higher order function you want is scanLeft, which is like a fold that remembers its intermediate steps. For example, say you have the following:

    val ls = Seq(1, 2, 3)
    def gen(lastValue: Double, n: Int) = math.pow(lastValue, 2) + n
    

    Then you can combine them like this with scanLeft:

    scala> ls.scanLeft(1.0)(gen)
    res0: Seq[Double] = List(1.0, 2.0, 6.0, 39.0)
    

    This is more or less equivalent to Apocalisp’s formulation with foldLeft, except that scanLeft takes care of holding on to the intermediate values for you.

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

Sidebar

Related Questions

The recursion is sort of a 'divide and conquer' style, it splits up while
I am learning about recursion tree's and trying to figure out how the height
How many maximum recursion level possible for CTE in SQL server? If maximum recursion
I am trying to use compile time recursion to output the elements of a
I need some help with recursion. I'm trying do a binary tree in C#,
I referred to several questions here about recursion but I am not able to
I'm trying to write a Python class that acts like some sort of datastore.
I am trying to make a recursion function that will print all the elements
I'm working with recursion trying to become better at with it. My current activity
With respect to using recursion over non-recursive methods in sorting algorithms or, for that

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.