-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExPerm.elm
71 lines (57 loc) · 1.3 KB
/
ExPerm.elm
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module ExPerm exposing (..)
import List
permutations : List a -> List (List a)
permutations input =
case input of
[] ->
[[]]
items ->
let
one (x,xs) =
List.map (\i -> x :: i) (permutations xs)
in
List.concatMap one (select items)
-- eg. [1,2,3] => [ (1,[2,3]), (2,[1,3]), (3,[1,2]) ]
select : List a -> List (a, List a)
select things =
case things of
[] -> []
(x :: xs) ->
(x, xs) :: (List.map (\(y,ys) -> (y, x :: ys)) (select xs))
-- let three = do
-- let elements = [1,2,3]
-- x <- elements
-- y <- elements
-- z <- elements
-- pure [x,y,z]
three : List a -> List (List a)
three elements =
List.concatMap (\x ->
List.concatMap (\y ->
List.concatMap (\z ->
[ [x, y, z] ]
) elements
) elements
) elements
-- let listReplicate times elements =
-- replicateM times elements
listReplicate : Int -> List a -> List (List a)
listReplicate times elements =
let
go count =
if count <= 0
then
[[]]
else
listLift2 (\a b -> a :: b)
elements
(go (count - 1))
in
go times
listApply fab xs =
case fab of
[] -> []
(f :: fs) ->
List.concat [(List.map f xs), (listApply fs xs)]
listLift2 f a b =
listApply (List.map f a) b