Re: [Haskell-cafe] Removing alternate items from a list

2010-06-10 Thread Markus Läll
Yep, the test is done by a rookie. If I get more time, I'll try to look into testing a little more, and redo the timing (if anyone doesn't do it firs) -- using optimizations, more runs per function and the criterion package. ___ Haskell-Cafe mailing list

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-10 Thread Sebastiaan Visser
Or, when lists had a decent eliminator defined in the Prelude (just like maybe for Maybe and either for Either): list :: b -> (a -> [a] -> b) -> [a] -> b list d _ [] = d list _ f (x:xs) = f x xs fromList = list [] we could write the alternate function like this: alt :: [a] -> [a] a

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-10 Thread John Lato
> From: Markus L?ll > > So out of curiosity i took the definitions given in this thread, and > tried to run timing-tests. > Here's what I ran: >> ghc -prof -auto-all -o Test Test.h >> Test +RTS -p > and then looked in the Test.prof file. I think this is a poor approach for timing tests, for two r

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-09 Thread John Meacham
On Wed, Jun 09, 2010 at 11:47:32PM +0300, Markus Läll wrote: > As the function doing (x:_:rest) pattern-matching was the fastest I > extended the idea from that to (x1:_:x2: ... x10:_:rest), but skipping > from 5 to 10, where all steps showed a small increase in performance. > > So a question: whe

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-09 Thread Yitzchak Gale
Markus Läll wrote: > So out of curiosity i took the definitions given in this thread, and > tried to run timing-tests. Nice! > Any comments? (besides -O2 ;-)  -- I remembered it too late and didn't > want to restart... Oh, could you please run that again with -O2? My entry dearly depends on it

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-09 Thread Markus Läll
Forgot the file -- here it is: module Main where import Data.Either (rights) import Data.Function (fix) test f = putStr $ show $ last $ f $ replicate 1000 (1 :: Int) main = test matchPattern4 -- 1. zipNums -- 2. matchPattern -- 3. zipBoolCycle -- 4. iterDrop -- 5. zipBoolCycle2 -- 6. consum

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-09 Thread Markus Läll
So out of curiosity i took the definitions given in this thread, and tried to run timing-tests. Here's what I ran: > ghc -prof -auto-all -o Test Test.h > Test +RTS -p and then looked in the Test.prof file. All tests I ran from 3 to 10 times (depending on how sure I wanted to be), so the results a

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Mike Dillon
OK, here's mine: f as = [ x | (True,x) <- zip (cycle [True, False]) as ] -md begin R J quotation: > > What's the cleanest definition for a function f :: [a] -> [a] that takes a > list and returns the same list, with alternate items removed? e.g., f [0, 1, > 2, 3, 4, 5] = [1,3,5]? >

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Yitzchak Gale
Christopher Done wrote: > Can't forget fix in a game of code golf! > >> (fix $ \f (x:_: xs) -> x : f xs) [1..] > => [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,4... Ho, good shot! It only works for infinite lists, though: Prelude> (fix $ \f (x:_: xs) -> x : f xs) [1..10] [1,3,5,7,9

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Ozgur Akgun
> > It only works for infinite lists, though > you wanted it :) (fix $ \f xs -> case xs of { (x:_: xs) -> x : f xs; _ -> [] }) [1..10] = [1,3,5,7,9] here you go :) 2010/6/8 Yitzchak Gale > Christopher Done wrote: > > Can't forget fix in a game of code golf! > > > >> (fix $ \f (x:_: xs) -> x :

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Jürgen Doser
El dom, 06-06-2010 a las 14:46 +, R J escribió: > What's the cleanest definition for a function f :: [a] -> [a] that > takes a list and returns the same list, with alternate items removed? > e.g., f [0, 1, 2, 3, 4, 5] = [1,3,5]? adding another suggestion: import Data.Either(rights) f = righ

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Stephen Tetley
Hello The regular list functionals - map, zip, foldl, foldr, filter, etc. - all process the input list at "speed 1" - i.e. one element at a time. As Ozgur Akgun has shown - consuming the list at "speed 2" gives a very pleasant implementation - algorithmically: "consume at speed 2, produce the new

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Christopher Done
On 8 June 2010 15:13, Jürgen Doser wrote: > El dom, 06-06-2010 a las 14:46 +, R J escribió: >> What's the cleanest definition for a function f :: [a] -> [a] that >> takes a list and returns the same list, with alternate items removed? >>  e.g., f [0, 1, 2, 3, 4, 5] = [1,3,5]? > > adding anothe

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Bill Atkins
Yes, this was an old draft I accidentally sent out. My post higher up the thread is correct. :) On Tuesday, June 8, 2010, Ozgur Akgun wrote: > if we add 'a' to the definition of this function, (to make it work), the type > of it turns out to be: [a] -> [(a, Bool)] > > you might have forgotten t

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Alexey Levan
2010/6/6 R J : > What's the cleanest definition for a function f :: [a] -> [a] that takes a > list and returns the same list, with alternate items removed?  e.g., f [0, > 1, 2, 3, 4, 5] = [1,3,5]? f x = [y | (True, y) <- zip (cycle [False, True]) x] ___

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread aditya siram
And for a few more lines of codes, you get a more flexible solution: data Consume = Take | Skip consumeBy :: [Consume] -> [a] -> [a] consumeBy [] _ = [] consumeBy _[] = [] consumeBy (tOrS:takesAndSkips) (x:xs) = case tOrS of

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Bill Atkins
f :: [a] -> [a] f = filter snd $ zip a (cycle [True, False]) On Monday, June 7, 2010, Ozgur Akgun wrote: > or, since you don't need to give a name to the second element of the list: > > f :: [a] -> [a] > f (x:_:xs) = x : f xsf x = x > > > > > On 7 June 2010 20:11, Ozgur Akgun wrote: > > i think

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Ozgur Akgun
if we add 'a' to the definition of this function, (to make it work), the type of it turns out to be: [a] -> [(a, Bool)] you might have forgotten the "map fst $" part. Best, On 8 June 2010 14:51, Bill Atkins wrote: > f :: [a] -> [a] > f = filter snd $ zip a (cycle [True, False]) > > On Monday,

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Christopher Done
Can't forget fix in a game of code golf! > (fix $ \f (x:_: xs) -> x : f xs) [1..] => [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,4... 2010/6/8 Yitzchak Gale : > R J wrote: >> What's the cleanest definition for a function f :: [a] -> [a] that takes a >> list and returns the same lis

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-08 Thread Yitzchak Gale
R J wrote: > What's the cleanest definition for a function f :: [a] -> [a] that takes a > list and returns the same list, with alternate items removed?  e.g., f [0, > 1, 2, 3, 4, 5] = [1,3,5]? f = map head . takeWhile (not . null) . iterate (drop 2) . drop 1 Regards, Yitz

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-07 Thread Bill Atkins
alts :: [a] -> [a] alts xs = map fst . filter snd $ zip xs (cycle [False, True]) Prelude> alts [0, 1..5] [1,3, 5] On Sunday Jun 6, 2010, at 10:46 AM, R J wrote: > What's the cleanest definition for a function f :: [a] -> [a] that takes a > list and returns the same list, with alternate items re

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-07 Thread Ozgur Akgun
or, since you don't need to give a name to the second element of the list: f :: [a] -> [a] f (x:_:xs) = x : f xs f x = x On 7 June 2010 20:11, Ozgur Akgun wrote: > i think explicit recursion is quite clean? > > > f :: [a] -> [a] > f (x:y:zs) = x : f zs > f x = x > > > > > On 7 June 2010 19:4

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-07 Thread Ozgur Akgun
i think explicit recursion is quite clean? f :: [a] -> [a] f (x:y:zs) = x : f zs f x = x On 7 June 2010 19:42, Thomas Hartman wrote: > maybe this? > > map snd . filter (odd . fst) . zip [1,2..] $ [1,2,3,4,5] > > 2010/6/6 R J : > > What's the cleanest definition for a function f :: [a] -> [a]

Re: [Haskell-cafe] Removing alternate items from a list

2010-06-07 Thread Thomas Hartman
maybe this? map snd . filter (odd . fst) . zip [1,2..] $ [1,2,3,4,5] 2010/6/6 R J : > What's the cleanest definition for a function f :: [a] -> [a] that takes a > list and returns the same list, with alternate items removed?  e.g., f [0, > 1, 2, 3, 4, 5] = [1,3,5]? > > ___

[Haskell-cafe] Removing alternate items from a list

2010-06-07 Thread R J
What's the cleanest definition for a function f :: [a] -> [a] that takes a list and returns the same list, with alternate items removed? e.g., f [0, 1, 2, 3, 4, 5] = [1,3,5]? _ The New Bus