Till Doerges writes:
I tried to implement a function that separates a list into two parts according to a list of indices given. (Does anything like that perhaps exist already in the Prelude?) ...
1) Could anybody please explain the behaviour of select and select' to me? To add the icing on the cake: How can I improve select?
You might try the following version that does not build up huge data structures in accumulating parameters to output them only in the two base cases. Rather, it produces partial output as early as possible. For huge lists, however, it might still fail, if GC cannot claim enough free space: select'' :: [a] -> [Integer] -> ([a],[a]) select'' xs poss = sAcc xs (sort poss) 0 where sAcc :: [a] -> [Integer] -> Integer -> ([a],[a]) sAcc [] _ _ = ([],[]) sAcc xs [] _ = (xs,[]) sAcc (x:xs) pl@(pos:poss) curpos = if (pos == curpos) then let (u,v) = sAcc xs poss (curpos+1) in (u,x:v) else let (u,v) = sAcc xs pl (curpos+1) in (x:u,v) -- Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de
Hi everybody, thanks for all the answers! On Fri, Oct 19, 2001 at 09:55:00AM +0200, Janis Voigtlaender wrote: [...]
You might try the following version that does not build up huge data structures in accumulating parameters to output them only in the two base cases. Rather, it produces partial output as early as possible. [...] select'' :: [a] -> [Integer] -> ([a],[a])
That one works just great and was pretty much, was I was looking for. (And looking at it, I *should* have been able to come up w/ such a solution myself...) ;-) Anything else, like reversing the lists, seems to be more complicated (leave alone my first attempts). And no, partition or groupBy don't do the trick for me (unless I really don't get, what they are supposed to do). Thanks -- Till -- e-mail: reverse(net dot doerges at till) | ENCRYPTED | pgp/gpg: keys via keyserver or my homepage | MAIL IS | www: http://www.doerges.net | WELCOME! |
participants (2)
-
Janis Voigtlaender -
Till Doerges