Daniel Fischer schrieb:
> However, according to a couple of tests, the "funkyName" version is somewhat 
> faster and 
> allocates less.

My timing tests showed that your fpairs version is fastest.
(first argument "True" selects filteredPairs, "False" funkyName)

My initial version "myf" is almost unusable.

C.

(code attached)

mae...@leibniz:~/haskell/examples> ghc --make -O2 FilteredPairs.hs
[1 of 1] Compiling Main             ( FilteredPairs.hs, FilteredPairs.o )
Linking FilteredPairs ...
mae...@leibniz:~/haskell/examples> time ./FilteredPairs True EQ 5000
5000

real    0m0.567s
user    0m0.536s
sys     0m0.020s
mae...@leibniz:~/haskell/examples> time ./FilteredPairs False EQ 5000
5000

real    0m0.819s
user    0m0.796s
sys     0m0.012s
import Data.Char
import System.Environment

filteredPairs :: (a -> b -> Bool) -> [a] -> [b] -> [[(a, b)]]
filteredPairs p s l = sequence [[(a, b) | b <- filter (p a) l] | a <- s]

pairs :: [a] -> [b] -> [[(a, b)]]
pairs l1 = map (zip l1) . takeKFromN l1

takeKFromN :: [b] -> [a] -> [[a]]
takeKFromN s l = case s of
  [] -> [[]]
  _ : r -> [ a : b | a <- l, b <- takeKFromN r l]

myf :: (a -> b -> Bool) -> [a] -> [b] -> [[(a, b)]]
myf p l = filter (all (uncurry p)) . pairs l

ordA = ord 'a'

prd :: Ordering -> Int -> Char -> Bool
prd o i c = case o of
  LT -> ord c - ordA < i
  _ -> compare (ord c - ordA + 1) i == o

funkyName :: (a -> b -> Bool) -> [a] -> [b] -> [[(a,b)]]
funkyName p s l
    = case s of
        (h:t) -> [(h,a):ys | a <- filter (p h) l, ys <- funkyName p t l]
        [] -> [[]]

testCase :: Bool -> Ordering -> Int -> [[(Int, Char)]]
testCase b o i =
  (if b then filteredPairs else funkyName) (prd o)
  [1 .. i] ['a' .. chr (ordA + i)]

main = do
  [arg1, arg2, arg3] <- getArgs
  print . length . last . take 20 $ testCase (read arg1) (read arg2) (read arg3)
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to