-
Notifications
You must be signed in to change notification settings - Fork 1
/
perm2.hs
33 lines (29 loc) · 914 Bytes
/
perm2.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
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
import Control.Egison
import System.Environment
import Control.Monad.Search
import Data.List
import Data.Maybe
perm2 :: Int -> [(Int, Int)]
perm2 n =
matchAll dfs [1 .. n] (Multiset Something) [[mc| $x : $y : _ -> (x, y) |]]
perm2Native :: Int -> [(Int, Int)]
perm2Native n = go [1 .. n] [] []
where
go [] _ acc = acc
go (x : xs) rest acc =
[ (x, y) | y <- rest ++ xs ] ++ go xs (rest ++ [x]) acc
main = do
[fn, n] <- getArgs
let fn' = read fn :: Int
let n' = read n :: Int
case fn' of
-- n'=5000, 0.462
1 -> print $ length $ perm2 n'
-- n'=5000, 0.444
2 -> print $ length $ perm2Native n'