The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

List::Gen::Haskell - the haskell prelude in perl5

SYNOPSIS

this module provides most of the functions in the haskell prelude that pertain to working with lists.

    # haskell:  fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

    use List::Gen::Haskell;

    $fibs = lazy 0, 1, zipWith {&sum} $fibs, tail $fibs;

    print "@$fibs[0 .. 10]\n"; # prints '0 1 1 2 3 5 8 13 21 34 55'

lazy provides the generic behavior of haskell's lazy list concatenation (the : and ++ operators). none of its elements are touched until they are needed. in general, all the functions in this package defer their execution until something is required of them. they also only touch their arguments at the latest possible time, which is how the co-recursion above works.

the functions in this module are a bit more flexible (perlish) than those in haskell. in most cases where a function expects a single generator, a list of values and/or generators can be provided, which will be preprocessed by lazy into a single generator.

when loaded, most of the functions in this package become methods for all generators. if a method of the same name already exists, the method from this package will be prefixed with hs_ . so filter is a method named ->hs_filter(...)

this library currently does not have the best performance due to over-caching of many internal generators. a future update will address this by replacing those generators with cache-less generator streams.

FUNCTIONS

Utility Operations

x_xs
x_xs GENERATOR

x_xs is a convenience function that returns the head and tail of a passed in generator. x_xs uses $_ without an argument.

seq LIST

forces immediate evaluation of the elements in LIST and returns the list

flip CODE

flip converts CODE into a function that takes it's arguments reversed

    my $idx = \&head . flip \&drop;

    $idx->($gen, 5)  ==  $gen->$idx(5)  ==  $gen->get(5)

List operations

map :: (a -> b) -> [a] -> [b]
map CODE LIST

map f xs is the list obtained by applying f to each element of xs, i.e.,

    map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]
    map f [x1, x2, ...] == [f x1, f x2, ...]

    $x = &map(sub {$_**2}, $gen);
    $x = Map {$_**2} $gen;

that usage is the same as List::Gen::gen , but this is something that gen can't do:

    my $pow_2 = Map {$_**2};  # partial application, needs at least 1 more
                              # argument to evaluate, but can be passed a list
    my $ints = <0..>;

    my $squares = $ints->$pow_2;

    say "@$squares[0 .. 10]"; # 0 1 4 9 16 25 36 49 64 81 100

and this:

    my $src;
    my $square_of_src = Map {$_ ** 2} $src;

    $src = <1.. by 2>;

    say "@$square_of_src[0 .. 4]"; # 1 9 25 49 81
filter :: (a -> Bool) -> [a] -> [a]

filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,

    filter p xs = [ x | x <- xs, p x]
head :: [a] -> a

Extract the first element of a list, which must be non-empty.

last :: [a] -> a

Extract the last element of a list, which must be finite and non-empty.

tail :: [a] -> [a]

Extract the elements after the head of a list, which must be non-empty.

init :: [a] -> [a]

Return all the elements of a list except the last one. The list must be non-empty.

null :: [a] -> Bool

Test whether a list is empty.

length :: [a] -> Int
reverse :: [a] -> [a]

reverse xs returns the elements of xs in reverse order. xs must be finite.

Reducing lists (folds)

foldl :: (a -> b -> a) -> a -> [b] -> a

foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

    foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn

The list must be finite.

foldl1 :: (a -> a -> a) -> [a] -> a

foldl1 is a variant of foldl that has no starting value argument, and thus must be applied to non-empty lists.

foldr :: (a -> b -> b) -> b -> [a] -> b

foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left:

    foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
foldr1 :: (a -> a -> a) -> [a] -> a

foldr1 is a variant of foldr that has no starting value argument, and thus must be applied to non-empty lists.

Special folds

and :: [Bool] -> Bool

and returns the conjunction of a Boolean list. For the result to be True, the list must be finite; False, however, results from a False value at a finite index of a finite or infinite list.

or :: [Bool] -> Bool

or returns the disjunction of a Boolean list. For the result to be False, the list must be finite; True, however, results from a True value at a finite index of a finite or infinite list.

any :: (a -> Bool) -> [a] -> Bool

Applied to a predicate and a list, any determines if any element of the list satisfies the predicate.

all :: (a -> Bool) -> [a] -> Bool

Applied to a predicate and a list, all determines if all elements of the list satisfy the predicate.

sum :: Num a => [a] -> a

The sum function computes the sum of a finite list of numbers.

product :: Num a => [a] -> a

The product function computes the product of a finite list of numbers.

concat :: [[a]] -> [a]

Concatenate a list of lists.

concatMap :: (a -> [b]) -> [a] -> [b]

Map a function over a list and concatenate the results.

maximum :: Ord a => [a] -> a

maximum returns the maximum value from a list, which must be non-empty, finite, and of an ordered type. It is a special case of maximumBy, which allows the programmer to supply their own comparison function.

minimum :: Ord a => [a] -> a

minimum returns the minimum value from a list, which must be non-empty, finite, and of an ordered type. It is a special case of minimumBy, which allows the programmer to supply their own comparison function.

Building lists

Scans

scanl :: (a -> b -> a) -> a -> [b] -> [a]

scanl is similar to foldl, but returns a list of successive reduced values from the left:

    scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]

Note that

    last (scanl f z xs) == foldl f z xs.
scanr :: (a -> b -> b) -> b -> [a] -> [b]

scanr is the right-to-left dual of scanl. Note that

    head (scanr f z xs) == foldr f z xs.

Infinite lists

iterate :: (a -> a) -> a -> [a]

iterate f x returns an infinite list of repeated applications of f to x:

    iterate f x == [x, f x, f (f x), ...]
repeat :: a -> [a]

repeat x is an infinite list, with x the value of every element.

hs_repeat :: a -> [a]
    my $repeat; $repeat = lazy $x, $repeat;
replicate :: Int -> a -> [a]

replicate n x is a list of length n with x the value of every element.

cycle :: [a] -> [a]

cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.

hs_cycle :: [a] -> [a]

hs_cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.

it is defined in perl as:

   my $cycle; $cycle = lazy $xs, $cycle;

Sublists

take :: Int -> [a] -> [a]

take n, applied to a list xs, returns the prefix of xs of length n, or xs itself if n > length xs:

    take 3 [1,2,3,4,5] == [1,2,3]
    take 3 [1,2] == [1,2]
    take 3 [] == []
    take (-1) [1,2] == []
    take 0 [1,2] == []
drop :: Int -> [a] -> [a]

drop n xs returns the suffix of xs after the first n elements, or [] if n > length xs:

    drop 3 [1,2,3,4,5] == [4,5]
    drop 3 [1,2] == []
    drop 3 [] == []
    drop (-1) [1,2] == [1,2]
    drop 0 [1,2] == [1,2]
splitAt :: Int -> [a] -> ([a], [a])

splitAt n xs returns a tuple where first element is xs prefix of length n and second element is the remainder of the list:

    splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])
    splitAt 1 [1,2,3] == ([1],[2,3])
    splitAt 3 [1,2,3] == ([1,2,3],[])
    splitAt 4 [1,2,3] == ([1,2,3],[])
    splitAt 0 [1,2,3] == ([],[1,2,3])
    splitAt (-1) [1,2,3] == ([],[1,2,3])

It is equivalent to (take n xs, drop n xs).

takeWhile :: (a -> Bool) -> [a] -> [a]

takeWhile, applied to a predicate p and a list xs, returns the longest prefix (possibly empty) of xs of elements that satisfy p:

    take_while (< 3) [1,2,3,4,1,2,3,4] == [1,2]
    take_while (< 9) [1,2,3] == [1,2,3]
    take_while (< 0) [1,2,3] == []
dropWhile :: (a -> Bool) -> [a] -> [a]

dropWhile p xs returns the suffix remaining after take_while p xs:

    dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3]
    dropWhile (< 9) [1,2,3] == []
    dropWhile (< 0) [1,2,3] == [1,2,3]
span :: (a -> Bool) -> [a] -> ([a], [a])

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:

    span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4])
    span (< 9) [1,2,3] == ([1,2,3],[])
    span (< 0) [1,2,3] == ([],[1,2,3])

span p xs is equivalent to (takeWhile p xs, dropWhile p xs)

break :: (a -> Bool) -> [a] -> ([a], [a])

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:

    break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4])
    break (< 9) [1,2,3] == ([],[1,2,3])
    break (> 9) [1,2,3] == ([1,2,3],[])

break p is equivalent to span (not . p)

Searching lists

elem :: Eq a => a -> [a] -> Bool

elem is the list membership predicate, usually written in infix form, e.g., x `elem` xs.

notElem :: Eq a => a -> [a] -> Bool

notElem is the negation of elem.

Zipping and unzipping lists

zip :: [a] -> [b] -> [(a, b)]

zip takes 2+ lists and returns a single interleaved list. If one input list is short, excess elements of the longer lists are discarded. unlike the haskell version, the zip returns a flat generator.

zip is the same as zipWith {\@_}

zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

zipWith generalizes zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two lists to produce the list of corresponding sums.

zipWithAB {$a * $b} $gen1, $gen2

The zipWithAB function takes a function which uses $a and $b , as well as two lists and returns a list analogous to zipWith.

unzip :: [a, b] -> ([a], [b])

unzip transforms a list into two lists of the even and odd elements.

    zs = zip xs, ys
    (xs, ys) == unzip zs
unzipn Int, ...

The unzipn function is the n-dimentional precursor to unzip

   unzip xs = unzipn 2, xs

Functions on strings

lines :: String -> [String]

lines breaks a string up into a list of strings at newline characters. The resulting strings do not contain newlines. the newline sequence is taken from the value of the input record separator $/

words :: String -> [String]

words breaks a string up into a list of words, which were delimited by white space.

unlines :: [String] -> String

unlines is an inverse operation to lines. It joins lines, after appending a terminating newline to each. the newline sequence is taken from the value of the input record separator $/

unwords :: [String] -> String

unwords is an inverse operation to words. It joins words with separating spaces.

ACKNOWLEGEMENTS

most of the documentation here started out at http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.html and was subsequently edited to account for implementation differences.

AUTHOR

Eric Strom, <asg at cpan.org>

BUGS

there are certainly bugs in code this complex. send in reports, tests, patches.

report any bugs / feature requests to bug-list-gen at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Gen.

COPYRIGHT & LICENSE

copyright 2009-2011 Eric Strom.

this program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

see http://dev.perl.org/licenses/ for more information.