Skip to content

Instantly share code, notes, and snippets.

@psqq
Last active January 7, 2016 18:32
Show Gist options
  • Save psqq/b77bf8faad7f0a5fbda4 to your computer and use it in GitHub Desktop.
Save psqq/b77bf8faad7f0a5fbda4 to your computer and use it in GitHub Desktop.
-- http://www.tutorialspoint.com/compile_haskell_online.php
import Data.Tree
import Data.List
encodeTree :: Tree t -> String
encodeTree (Node root []) = "01"
encodeTree (Node root childs) = "0" ++ intercalate "" encodeChilds ++ "1"
where encodeChilds = sort $ map encodeTree childs
isIsomorphic :: Tree t1 -> Tree t2 -> Bool
isIsomorphic t1 t2 = encodeTree t1 == encodeTree t2
op :: [[Int]] -> [Int] -> Int
op m [x] = x
op m (x:y:xs) = op m $ (m !! x !! y):xs
down m = op m [0, 1..length m - 1]
semigroupToTree :: [[Int]] -> Tree Int
semigroupToTree [[x]] = Node x []
-- Тестирование функций
main = do
print $ encodeTree (Node 1 [Node 2 [Node 3 [], Node 4 []], Node 5 []])
let t1 = Node 1 [Node 11 [], Node 12 [Node 121 [], Node 122 []]]
let t2 = Node 2 [Node 21 [Node 211 [], Node 212 []], Node 22 []]
let t3 = Node 3 [Node 31 [], Node 32 [Node 321 [], Node 322 [], Node 323 []]]
print $ encodeTree t1
print $ encodeTree t2
print $ encodeTree t3
print $ t1 == t2
print $ isIsomorphic t1 t2
print $ isIsomorphic t1 t3
print $ isIsomorphic t2 t3
let m1 = [[0, 0], [0, 1]]
let m2 = [[0, 1], [1, 1]]
print $ down m1
print $ down m2
-- Вывод программы:
-- "0001011011"
-- "0001011011"
-- "0001011011"
-- "000101011011"
-- False
-- True
-- False
-- False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment