[Haskell-beginners] take until duplicate
mike h
mike_k_houghton at yahoo.co.uk
Sun Sep 24 19:08:40 UTC 2017
I’m looking at how to take from a list until a duplicate is found.
e.g.
takeUntilDup [1,2,3,4,3] = [1,2,3,4]
I found this implementation
takeUntilDup = foldr (\x r -> x : takeWhile (/= x) r) []
It works but I can’t see how!!?
The accumulated result is built up in r so with input [1,2,3,4,3] then, at the point when r = [1, 2, 3, 4], the fold is about to use the number 3. i.e. it does takeWhile (/=3) [1,2,3,4] which gives [1,2]
Please, how does this work?
Thanks
Mike
.
More information about the Beginners
mailing list