[Haskell-beginners] foldr with short circuit and accumulator
Theodore Lief Gannon
tanuki at gmail.com
Tue Feb 2 08:47:02 UTC 2016
If you mean is there any f and z for which this can be done with only
"foldr f z xs", I believe the answer is no. If you don't mind extra parts,
findAdjacent :: (Eq a) => [a] -> Maybe a
findAdjacent xs = foldr f Nothing $ zip xs ps
ps = zipWith (==) (tail xs) xs
f (x,p) next = if p then Just x else next
On Mon, Feb 1, 2016 at 11:15 PM, Chul-Woong Yang <cwyang at aranetworks.com>
> Hi, all.
> Can it be possible to do fold with short circuit and accumulator both?
> For example, can we find an element which is same value to adjacent one?
> findAdjacent [1,2..n, n, n+1, n+2.......] => n
> \__very long__/
> Though there can be many ways to do it, Can we do it with fold[r|l]?
> I'll be happy to receive any comments.
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners