[Issue 11084] std.algorithm.scan

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Nov 11 05:10:10 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11084



--- Comment #3 from bearophile_hugs at eml.cc 2013-11-11 05:10:07 PST ---
An example that shows the usage and usefulness of scanl1/scanr1 in Haskell:
http://philipnilsson.github.io/Badness10k/articles/waterflow/


Prelude> let h = [2,5,1,2,3,4,7,7,6]
Prelude> scanl1 max h
[2,5,5,5,5,5,7,7,7]
Prelude> scanr1 max h
[7,7,7,7,7,7,7,7,6]
Prelude> zipWith min (scanl1 max h) (scanr1 max h)
[2,5,5,5,5,5,7,7,6]
Prelude> let level h = zipWith min (scanl1 max h) (scanr1 max h)
Prelude> zipWith (-) (level h) h
[0,0,4,3,2,1,0,0,0]
Prelude> let water h = sum $ zipWith (-) (zipWith min (scanl1 max h) (scanr1
max h)) h
Prelude> water h
10


Where 'scanl1' is a variant of 'scanl' that has no starting value argument:
> scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]


scanl1 :: (a -> a -> a) -> [a] -> [a]
scanl1 f (x:xs) =  scanl f x xs
scanl1 _ []     =  []


And 'scanr1' is a variant of 'scanr' that has no starting value argument:

scanr1 :: (a -> a -> a) -> [a] -> [a]
scanr1 _ []     =  []
scanr1 _ [x]    =  [x]
scanr1 f (x:xs) =  f x q : qs
                   where qs@(q:_) = scanr1 f xs 


In Phobos there are no functions sum ( Issue 4725 ), no zipWith ( Issue 8715 )
and no scanl/scanr/scanl1/scanr1 (this issue), so it takes some more code to
translate that Haskell solution to D.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list