Taking the rest of the list into account in filter

Stubborn

I need to display the number of elements whose successor is greater in a list. For example, in the list [3,7,2,1,9] my function should return 2 because 7 is greater than 3 and 9 is greater than 1.
In order to do that,I was thinking to use the filter function:

greaterElems :: Ord a => [a] -> Int
greaterElems [] = 0
greaterElems [x] = 0
greaterElems (x:xs) = length (filter (< head xs) (x:xs))

However, this does not work as expected: it seems that Haskell always considers the second element of the list, as if "head xs" is calculated only once, but this does not seem normal to me since Haskell is lazy.
What am I missing and how could I fix my code in order to achieve my goal?

Willem Van Onsem

You can make use of zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] where we pass the list, and its tail. Indeed:

sucGreater :: Ord a => [a] -> [Bool]
sucGreater x = zipWith (<) x (tail x)

or as @RobinZigmond says, we can omit tail, and use drop:

sucGreater :: Ord a => [a] -> [Bool]
sucGreater x = zipWith (<) x (drop 1 x)

For the given sample list, this gives us:

Prelude> sucGreater [3,7,2,1,9]
[True,False,False,True]

I leave it as an exercise to the count the number of Trues in that list.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Filter an array taking into account that the filtered options are optional

How to Sort a list taking in account multiple factors?

django: using Q to filter taking into account blank choice scenario

How to filter a dataframe taking into account two columns with different labels

list.files taking account of list number in R?

How to combine elements from the list taking into account index of the list?

Django Filter taking list of foreign key model

Create a list out of the keys in a dictionary taking into account their corresponding value

list.files taking account of file size in R?

jQuery: how to sort list text taking into account numerical values

Flutter : Sort a list alphabetically taking "accented characters" into account

filter table based on join but select all joins without taking filter into account

Not taking into account condition

initWithCoder Not Taking Into Account Autolayout

GeoDjango, distance filter based on database column taking into account the radius of both the seeker and the one he is looking for

use Streams to generate a new list taking into account all the first list elements

How would I split a string a string into a list of words, punctuation and spaces? (taking apostrophes into account)

How do I match exact strings from a list to a larger string taking white spaces into account?

How to create new column based on first column taking into account size of letter and list in Python Pandas?

Choose 14 combinations from 100 including all the numbers found in the list, and taking into account their frequency of appearance

How to filter by a list in Django REST Framework?

AccessController not taking a class' ProtectionDomain into account

http interceptor order not taking into account

Calculating collisions, taking into account velocity

Taking into account slight differences in color

Taking experimental errors into account in lmfit

Plot hours taking into account dates

MySQL query not taking GROUP BY into account

Is there an google (REST) API to list the Admin Roles in a Google Apps account?

TOP Ranking

HotTag

Archive