span function in Haskell

filter haskell
haskell list functions
haskell functions
map haskell
elem haskell
haskell modules
foldr haskell
haskell list comprehension

I think that the span function is Haskell is to apply a predicate to a list, and return a tuple where the first element is elements in the list that satisfy that predicate and the second element is the reminder of the list.

And it works well when I put: span (<3) [1,2,4,5,6]. It just returns in GHCI: ([1,2], [4,5,6]).

However, when I enter span (>3) [1,2,4,5,6], it returns ([],[1,2,4,5,6]). But I thought it should return ([4,5,6],[1,2]). So I was wondering the reason of it .

Your understanding of span is not entirely correct, this is what the official docs say:

span, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that satisfy p and second element is the remainder of the list

(emphasis mine).

Hence, the predicate is applied to each element of the list starting from the beginning. This means that the predicate in

span (<3) [1,2,4,5,6]

is satisfied for the first two elements, and the result is

([1,2], [4,5,6])

But in this other example

span (>3) [1,2,4,5,6]

the first element of the list already doesn't satisfy the predicate, so the first element of the returned tuple will be an empty list.

[Haskell-cafe] Span function, Basic functions; List transformations; Reducing lists (folds) span p xs is equivalent to ( takeWhile p xs, dropWhile p xs) isInfixOf "Ial" "I really like Haskell. Example 1. Input: splitAt 5 [1,2,3,4,5,6,7,8,9,10] Output: ([1,2,3,4,5],[6,7,8,9,10]) ([1,2,3,4,5],[6,7,8,9,10])

What you describe here is partition :: (a -> Bool) -> [a] -> ([a], [a]). This is a function that for a given predicate will take a list, and make a 2-tuple where the first item is a list with items that satisfy the predicate, and the second item a list of items that do not satisfy the predicate. Indeed:

Prelude Data.List> partition (>3) [1,2,4,5,6]

span :: (a -> Bool) -> [a] -> ([a], [a]) on the other hand makes a 2-tuple where the first item is the longest prefix of elements in the list that satisfy the predicate, and the second item is the list of remaining elements. Since for span (>3) [1,2,4,5,6], the first item does not satisfy the predicate. The longest prefix is the empty list [], and all elements of the given list, appear in the second item.

Data.List - Hackage, I think they do that here because it allows them to 're-use' the memory that is taken up by [] . For example, span _ [] = ([], []) may lead to two thunks representing []� If the value is Left a, apply the first function to a; if it is Right b, apply the second function to b. Examples Expand. We create two values of type Either String Int, one using the Left constructor and another using the Right constructor. Then we apply "either" the length function (if we have a String) or the "times-two" function (if we have an Int):

Basically span's complement is kind of break :: (a -> Bool) -> [a] -> ([a], [a]). You might even need to read the part twice in the docs to understand the subtle difference between break and span.

break, applied to a predicate p and a list xs, returns a tuple where first element is longest prefix (possibly empty) of xs of elements that do not satisfy p and second element is the remainder of the list:

So coming back to your question

λ> break (>3) [1,2,4,5,6]

You may of course swap :: (a, b) -> (b, a) the tuple if that's essential. ie. swap . break

Understanding the span function and '@' - haskell, A Haskell module is a collection of related functions, types and typeclasses. Whereas span spans the list while the predicate is true, break breaks it when the � The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. For example,

Modules, This document summarizes a number of common Haskell functions defined in The function span p xs is equivalent to (takeWhile p xs, dropWhile p xs), while� The zipWith3 function takes a function which combines three elements, as well as three lists and returns a list of their point-wise combination, analogous to zipWith. It is capable of list fusion, but it is restricted to its first list argument and its resulting list.

A Tour of the Haskell Prelude (and a few other basic functions), In this appendix the entire Haskell Prelude is given. iterate, repeat, replicate, cycle, take, drop, splitAt, takeWhile, dropWhile, span, break, lines, words, unlines, � In order to summarize the different types of patterns for pattern matching, we can study the definitions of three standard Haskell functions: span, unzip, and fromMaybe. First, let’s consider the span function, which splits a list into the longest prefix of elements that satisfy a predicate and the remainder of the list, as defined in GHC.List:

[PDF] Haskell Quick Reference, Let's look at some typical functions on lists and see how their type can be span' :: Eq a => a -> List a -> (List a, List a) span' w Nil = ( Nil, Nil) span' w (x `Cons`� Converts an association list to a string. The string will have one pair per line, with the key and value both represented as a Haskell string. This function is designed to work with [(String, String)] association lists, but may work with other types as well.

  • Thanks! And I was just wondering if there is a function in Haskell that work in the way as I said, rather that prefix match?
  • Sure! See Willem's answer