[Haskell-cafe] List spine traversal
Graham Fawcett
graham.fawcett at gmail.com
Mon Jun 29 12:30:20 EDT 2009
On Sun, Jun 28, 2009 at 10:05 PM, Tony Morris<tonymorris at gmail.com> wrote:
> Is there a canonical function for traversing the spine of a list?
>
> I could use e.g. (seq . length) but this feels dirty, so I have foldl'
> (const . const $ ()) () which still doesn't feel right. What's the
> typical means of doing this?
You can also use the combinators in Control.Parallel.Strategies.
'seqList r0' will force the spine, but you also can swap in
different strategies to force evaluation on the elements as well.
Prelude Control.Parallel.Strategies> let b = [ [1..n] | n <- [1..5] ]
Prelude Control.Parallel.Strategies> :print b
b = (_t1::[[Integer]])
Prelude Control.Parallel.Strategies> seqList r0 b
()
Prelude Control.Parallel.Strategies> :print b
b = [(_t2::[Integer]),(_t3::[Integer]),(_t4::[Integer]),
(_t5::[Integer]),(_t6::[Integer])]
Prelude Control.Parallel.Strategies> seqList rwhnf b
()
Prelude Control.Parallel.Strategies> :print b
b = [(1 : (_t7::[Integer])),(1 : (_t8::[Integer])),
(1 : (_t9::[Integer])),(1 : (_t10::[Integer])),
(1 : (_t11::[Integer]))]
Prelude Control.Parallel.Strategies> seqList rnf b
()
Prelude Control.Parallel.Strategies> :print b
b = [[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]
I'm a newbie, but I believe that a typical usage would be
something like:
let myList = someExpression `using` seqList r0
where 'using' ensures traversal of the expression's spine.
Graham
More information about the Haskell-Cafe
mailing list