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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:45:56+00:00 2026-05-30T18:45:56+00:00

In a programming language that is purely functional (like Haskell) or where you are

  • 0

In a programming language that is purely functional (like Haskell) or where you are only using it in a functional way (eg clojure); suppose you have a list/seq/enumerable (of unknown size) of integers and you want to produce a new list/seq/enumerable that contains the differences between successive items, how would you do it?

What I did previously in C# was to fold over the list and keep a state object as the aggregating value which recorded the ‘previous’ item so that you could do a diff on it from the current item. The the result list also had to go into the state object (which is a problem for a list of unknown size)

What is the general approach for doing this kind of thing functionally?

  • 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-30T18:45:57+00:00Added an answer on May 30, 2026 at 6:45 pm

    In Haskell you would probably just use some higher order function like zipWith. So you could do something like this:

    diff [] = []
    diff ls = zipWith (-) (tail ls) ls
    

    Note how I handled the [] case separately–if you pass an empty list to tail you get a runtime error, and Haskellers really, really hate runtime errors. However, in my function, I’m guaranteed the ls is not empty, so using tail is safe. (For reference, tail just returns everything except the first item of the list. It’s the same as cdr in Scheme.)

    This just takes the list and its tail and combine all of the items using the (-) function.

    Given a list [1,2,3,4], this would go something like this:

    zipWith (-) [2,3,4] [1,2,3,4]
    [2-1, 3-2, 4-3]
    [1,1,1]
    

    This is a common pattern: you can compute surprisingly many things by cleverly using standard higher-order functions. You are also not afraid of passing in a list and its own tail to a function–there is no mutation to mess you up and the compiler is often very clever about optimizing code like this.

    Coincidentally, if you like list comprehensions and don’t mind enabling the ParallelListComp extension, you could write zipWith (-) (tail ls) ls like this:

    [b - a | a <- ls | b <- tail ls]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any open source or commercial web programming language that function much like
If you've bought into the functional programming paradigm, the chances are that you like
I'm quite new to that functional programming paradigm, but so far I like it.
Are data structures like linked lists something that are purely academic for real programming
Do you know any programming language that implements abstract machines like Turing machines and
I have a program written in the Delphi Programming Language that I have to
I want to learn a new programming language. I have in mind stuff like
I am looking for a programming language that is fast like C and C++
I'm trying to write a scanner for my C/C++/C#/Java/D-like programming language that I'm designing
It's fairly obvious why a functional programming language that wants to be lazy needs

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.