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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:42:23+00:00 2026-06-12T15:42:23+00:00

Several times I’ve wanted to traverse a list and pick out elements that have

  • 0

Several times I’ve wanted to traverse a list and pick out elements that have some property which also relies on, say, the next element in the list. For a simple example I have some code which counts how many times a function f changes sign over a specified interval [a,b]. This is fairly obvious in an imperative language like C:

for(double x=a; x<=b; x+=(b-a)/n){
    s*f(x)>0 ? : printf("%e %e\n",x, f(x)), s=sgn(f(x));
    }

In Haskell my first instinct was to zip the list with its tail and then apply the filter and extract the elements with fst or whatever. But that seems clumsy and inefficient, so I shoehorned it into being a fold:

signChanges f a b n = tail $       
    foldl (\(x:xs) y -> if (f x*f y)<0 then y:x:xs else x:xs) [a] [a,a+(b-a)/n..b]

Either way I feel there is a “right” way to do this (as there so often is in Haskell) and that I don’t know (or just haven’t realised) what it is. Any help with how to express this in a more idiomatic or elegant way would be greatly appreciated, as would advice on how, in general, to find the “right” way to do things.

  • 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-12T15:42:24+00:00Added an answer on June 12, 2026 at 3:42 pm

    Here is a “version” using a paramorphism (not quite the same as the question – but it should illustrate a paramorphism usefully enough), first we need para as it is not in the standard libraries:

    -- paramorphism (generalizes fold)
    para :: (a -> ([a], b) -> b) -> b -> [a] -> b
    para phi b = step
      where step []     = b
            step (x:xs) = phi x (xs, step xs)
    

    Using a paramorphism is much like using a fold but as well as seeing the accumulator we can see the rest of input:

    countSignChanges :: [Int] -> Int
    countSignChanges = para phi 0
      where
        phi x ((y:_),st)  = if signum x /= signum y then st+1 else st
        phi x ([],   st)  = st
    
    
    demo = countSignChanges [1,2,-3,4,-5,-6] 
    

    The nice thing about para compared to zipping against the tail is that we can peek as far as we want into the rest of input.

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

Sidebar

Related Questions

Several times while debugging a VB.Net program I have found that continuation lines are
Several times I have wanted to detect single keystrokes in R but have failed
I have done this several times before but for some reason I can't get
I have heard this several times, that one should always update before committing to
Several times in my career, I have worked in a software group that determined
I have read several times that after you delete a row in an InnoDB
Several times I heard that PHP hosting is much cheaper than ASP.NET one. I
I've run into this problems several times before when trying to do some html
I have seen the following exception case several times public SomeClass(IEnumerable<T> someValues) { if
I have a certain loop occurring several times in various functions in my code.

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.