Skip to content

Instantly share code, notes, and snippets.

@KeenS
Created December 20, 2017 15:16
Show Gist options
  • Select an option

  • Save KeenS/8ee9c1c54672b017bd3599b274770666 to your computer and use it in GitHub Desktop.

Select an option

Save KeenS/8ee9c1c54672b017bd3599b274770666 to your computer and use it in GitHub Desktop.
$ ghc -O3 fold_elem.hs
$ time ./fold_elem foldr
True
./fold_elem foldr 1.59s user 0.01s system 99% cpu 1.602 total
$ time ./fold_elem foldl
True
./fold_elem foldl 2.96s user 0.01s system 99% cpu 2.978 total
$ time ./fold_elem rec
True
./fold_elem rec 1.63s user 0.00s system 99% cpu 1.638 total
module Main
where
import System.Environment
elem_foldr:: (Eq a, Foldable t) => a -> t a -> Bool
elem_foldr x = foldr (\y acc -> x == y || acc) False
elem_foldl:: (Eq a, Foldable t) => a -> t a -> Bool
elem_foldl x = foldl (\acc y -> acc || x == y) False
elem_rec:: Eq a => a -> [a] -> Bool
elem_rec _ [] = False
elem_rec x (y:ys) = x == y || elem_rec x ys
main:: IO ()
main = do
-- let e = 100000000::Integer
-- let e = 1::Integer
let e = 200000000::Integer
let list = [1..200000000]
args <- getArgs
case head args of
"foldr" -> putStrLn $ show $ elem_foldr e list
"foldl" -> putStrLn $ show $ elem_foldl e list
"rec" -> putStrLn $ show $ elem_rec e list
_ -> return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment