-
Notifications
You must be signed in to change notification settings - Fork 1
/
comb2.hs
45 lines (39 loc) · 1.22 KB
/
comb2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
import Control.Egison
import Data.List
import System.Environment
import Data.Maybe
comb2 :: Int -> [(Int, Int)]
comb2 n = matchAll dfs
[1 .. n]
(List Something)
[[mc| _ ++ $x : _ ++ $y : _ -> (x, y) |]]
comb2Native :: Int -> [(Int, Int)]
comb2Native n = [ (x, y) | (x : ts) <- tails [1 .. n], y <- ts ]
comb2Backtracking :: Int -> [(Int, Int)]
comb2Backtracking n =
toList
$ dfs [1 .. n]
>>= fromList
. tails
>>= (fromList . maybeToList . uncons)
>>= \(x, ys) ->
fromList (tails ys)
>>= (\ys' -> fromList (maybeToList (uncons ys)))
>>= \(y, _) -> pure (x, y)
main = do
[fn, n] <- getArgs
let fn' = read fn :: Int
let n' = read n :: Int
case fn' of
-- n'=15000, 0.303
1 -> print $ length $ comb2 n'
-- n'=15000, 0.347
2 -> print $ length $ comb2Native n'
-- n'=15000, 0.364
3 -> print $ length $ comb2Backtracking n'