
Dear All, I know this must be a one-liner, but it am banging my head against the wall. I am trying my hands at list manipulation in Haskell and a lot of useful function are making my life easier but I cannot achieve something really simple. Let us say you have the lists ml and sel ml=[23,44,55,8,98] and sel=[1,2] . Now, I would like simply to get a new list whose entries are the elements of ml in position sel. In my case things might be a little more complicated because all the elements are Integer and not Int (I noticed that sometimes this means I have to resort to generic functions). Cheers Lorenzo

well,
selFn = map (\x -> (! x)) sel -- This is not necessarily the most elegant way to word it would return a list of functions, which, when applied to a list, return an element from it based on the index results = map ($ ml) selFn would solve your problem
alternatively (and more cleanly),
results = map (ml !) sel
On Fri, Sep 10, 2010 at 10:55 AM, Lorenzo Isella
Dear All, I know this must be a one-liner, but it am banging my head against the wall. I am trying my hands at list manipulation in Haskell and a lot of useful function are making my life easier but I cannot achieve something really simple. Let us say you have the lists ml and sel
ml=[23,44,55,8,98] and sel=[1,2] .
Now, I would like simply to get a new list whose entries are the elements of ml in position sel. In my case things might be a little more complicated because all the elements are Integer and not Int (I noticed that sometimes this means I have to resort to generic functions). Cheers
Lorenzo _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Alex R

On 09/10/2010 05:03 PM, Alex Rozenshteyn wrote:
alternatively (and more cleanly),
results = map (ml !) sel
Thanks Alex. I am left with this problem only: the entries of sel are (in my code) Integer rather than Int so...I get an error message when compiling sine "!!" (I believe you left out a "!") expects an Int instead of an Integer. Any idea about how to fix this without converting sel from Integer to Int? Many thanks Lorenzo

You're right, I meant (!!). I've been playing with Array values recently,
and (!) is used there.
As has already been stated, genericIndex in the Data.List module will help
with the Integer problem, as will fromInteger.
As has also already been mentioned, lists + indexing = slow. If you care
about efficiency, and you need indexing, you might want to look into
Data.Array.IArray if you have fixed bounds or Data.Sequence if you don't.
On Fri, Sep 10, 2010 at 11:12 AM, Lorenzo Isella
On 09/10/2010 05:03 PM, Alex Rozenshteyn wrote:
alternatively (and more cleanly),
results = map (ml !) sel
Thanks Alex. I am left with this problem only: the entries of sel are (in my code) Integer rather than Int so...I get an error message when compiling sine "!!" (I believe you left out a "!") expects an Int instead of an Integer. Any idea about how to fix this without converting sel from Integer to Int? Many thanks
Lorenzo
-- Alex R

On Friday 10 September 2010 16:55:43, Lorenzo Isella wrote:
Dear All, I know this must be a one-liner, but it am banging my head against the wall. I am trying my hands at list manipulation in Haskell and a lot of useful function are making my life easier but I cannot achieve something really simple. Let us say you have the lists ml and sel
ml=[23,44,55,8,98] and sel=[1,2] .
Now, I would like simply to get a new list whose entries are the elements of ml in position sel.
Simple but potentially very inefficient: map (ml !!) sel resp. map (genericIndex ml) sel As long as all elements of sel are small, that's okay, but imagine sel = [k*10^6 | k <- [1 .. 10]] or worse, sel = [10^6 + k | k <- [0, 3 .. 1000]] If sel is increasing, selection :: Integral a => [a] -> [b] -> [b] selection sel = pick distances where distances = zipWith (-) sel (0:sel) pick [] _ = [] pick (d:ds) xs = case genericDrop d xs of [] -> [] ys@(y:_) -> y : pick ds ys *Select> selection [1,1,7,9,12,12,20] [0 .. 16] [1,1,7,9,12,12] *Select> selection [1,1,7,9,12,12,20] [0 .. 1000] [1,1,7,9,12,12,20] If sel is not increasing, an efficient solution is more complex.
In my case things might be a little more complicated because all the elements are Integer and not Int (I noticed that sometimes this means I have to resort to generic functions). Cheers
Lorenzo

On 2010-09-10, at 10:55 , Lorenzo Isella wrote:
Dear All, I know this must be a one-liner, but it am banging my head against the wall. I am trying my hands at list manipulation in Haskell and a lot of useful function are making my life easier but I cannot achieve something really simple. Let us say you have the lists ml and sel
ml=[23,44,55,8,98] and sel=[1,2] .
Now, I would like simply to get a new list whose entries are the elements of ml in position sel. In my case things might be a little more complicated because all the elements are Integer and not Int (I noticed that sometimes this means I have to resort to generic functions). Cheers
What about nested list comprehensions: [x|(x,y)<-zip ml [x `elem` sel | x <- [1..maximum sel]], y] Henry
participants (4)
-
Alex Rozenshteyn
-
Daniel Fischer
-
Henry Olders
-
Lorenzo Isella