-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAux.hs
65 lines (49 loc) · 1.89 KB
/
Aux.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-
Auxilary functions and data types for Gemia
Copyright (C) 2015 Sascha Fendrich
This file is part of Gemia.
Gemia is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gemia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gemia. If not, see <http://www.gnu.org/licenses/>.
-}
{-|
Module : $Header$
Description : General auxilary stuff for Gemia.
Copyright : Sascha Fendrich
License : GPL-3
Maintainer : [email protected]
Stability : experimental
Portability : portable
The Aux module declares some general stuff needed in Gemia, like
binary trees and making pairs.
-}
module Aux where
import Graphviz
-- Binary trees
data BTree a = Leaf a | Branch (BTree a) (BTree a) deriving (Eq,Show)
instance (Ord a) => Ord (BTree a) where
compare (Leaf a) (Leaf b) = compare a b
compare (Branch a1 a2) (Branch b1 b2)
| compare a1 b1 == LT = LT
| compare a1 b1 == EQ = compare a2 b2
| otherwise = GT
instance (Dotable n) => Dotable (BTree n) where
toDot (Leaf a) = toDot a
toDot (Branch a b) = "(" ++ (toDot a) ++ "," ++ (toDot b) ++ ")"
-- List of pairs
makePairsBy :: (a -> b -> c) -> [a] -> [b] ->[c]
makePairsBy f as bs = [f a b|a<-as,b<-bs]
-- List of all possible choices of elements from a list of lists
choices :: [[a]] -> [[a]]
choices [] = [[]]
choices (x:xs) = concatMap (\y -> prependAll y (choices xs)) x
-- Prepend an element to each list in a list of list
prependAll :: a -> [[a]] -> [[a]]
prependAll x ys = map (x:) ys