[commit: base] master: Less strict inits and tails (f5937ed)

Ian Lynagh igloo at earth.li
Sun Apr 3 19:55:08 CEST 2011


Repository : ssh://darcs.haskell.org//srv/darcs/packages/base

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/f5937ede7984724880a82d6bab56f81a5b1e1f80

>---------------------------------------------------------------

commit f5937ede7984724880a82d6bab56f81a5b1e1f80
Author: Ian Lynagh <igloo at earth.li>
Date:   Sun Apr 3 17:08:46 2011 +0100

    Less strict inits and tails
    
    Converted from darcs patches from Bas van Dijk <v.dijk.bas at gmail.com>
    
    Previously: tails _|_ = _|_
    Now:        tails _|_ = _|_ : _|_
    
    Previously: inits _|_ = _|_
    Now:        inits _|_ = [] : _|_

>---------------------------------------------------------------

 Data/List.hs |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/Data/List.hs b/Data/List.hs
index 061ad42..bb71da5 100644
--- a/Data/List.hs
+++ b/Data/List.hs
@@ -744,19 +744,24 @@ groupBy eq (x:xs)       =  (x:ys) : groupBy eq zs
 --
 -- > inits "abc" == ["","a","ab","abc"]
 --
+-- Note that 'inits' has the following strictness property:
+-- @inits _|_ = [] : _|_@
 inits                   :: [a] -> [[a]]
-inits []                =  [[]]
-inits (x:xs)            =  [[]] ++ map (x:) (inits xs)
+inits xs                =  [] : case xs of
+                                  []      -> []
+                                  x : xs' -> map (x :) (inits xs')
 
 -- | The 'tails' function returns all final segments of the argument,
 -- longest first.  For example,
 --
 -- > tails "abc" == ["abc", "bc", "c",""]
 --
+-- Note that 'tails' has the following strictness property:
+-- @tails _|_ = _|_ : _|_@
 tails                   :: [a] -> [[a]]
-tails []                =  [[]]
-tails xxs@(_:xs)        =  xxs : tails xs
-
+tails xs                =  xs : case xs of
+                                  []      -> []
+                                  _ : xs' -> tails xs'
 
 -- | The 'subsequences' function returns the list of all subsequences of the argument.
 --





More information about the Libraries mailing list