Changeset 12768

Show
Ignore:
Timestamp:
31.10.2009 15:21:50 (3 weeks ago)
Author:
maeder
Message:

added replace function

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/Common/Utils.hs

    r12314 r12768  
    1515module Common.Utils 
    1616  ( isSingleton 
     17  , replace 
    1718  , hasMany 
    1819  , number 
     
    4748import Control.Monad 
    4849 
     50-- | replace first (non-empty) sublist with second one in third argument list 
     51replace :: Eq a => [a] -> [a] -> [a] -> [a] 
     52replace sl r = case sl of 
     53  [] -> error "Common.Utils.replace: empty list" 
     54  _ -> replaceBy $ \ l@(hd : tl) -> case stripPrefix sl l of 
     55    Nothing -> ([hd], tl) 
     56    Just rt -> (r, rt) 
     57 
     58replaceBy :: ([a] -> ([b], [a])) -> [a] -> [b] 
     59replaceBy splt l = case l of 
     60  [] -> [] 
     61  _ -> let (ft, rt) = splt l in 
     62    ft ++ replaceBy splt rt 
     63 
    4964-- | add indices to a list starting from one 
    5065number :: [a] -> [(a, Int)]