Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module hash
- import StdEnv, StdLib, GenEq
- derive gEq HT, TType
- :: Hash :== Int
- :: HT k v = HT {[(k,v)]}
- :: TType a b = T1 a a (TType a b)
- | T2 b b (TType a b)
- | End
- test_HT_1 :: HT Int String
- test_HT_1 =
- HT { [(0,"Caspian"), (-10,"Joe Satriani")]
- , [(1,"Yngwie Malmsteen"), (6,"Motley Crue")]
- , [(-8,"Black Sabbath"), (2,"Iron Maiden")]
- , [(123,"Skindred"), (3,"Sabaton"), (-7,"Nine Inch Nails")]
- , [(4,"Dire Straits")]
- }
- test_HT_2 :: HT String (Int,String)
- test_HT_2 =
- HT { [("",(2,"Dire Straits"))]
- , [("cat",(10,"Skindred")), ("cowsay",(5,"Joe Satriani"))]
- , [("cp",(9,"Sabaton"))]
- , [("ps aux",(3,"Iron Maiden")), ("wget",(1,"Motley Crue"))]
- , []
- , [("rm",(7,"Nine Inch Nails")), ("ls",(6,"Black Sabbath"))]
- , [("mv",(8,"Caspian")), ("top",(4,"Yngwie Malmsteen"))]
- }
- test_HT_3 :: HT Int String
- test_HT_3 =
- HT { [(122,"felis silvestris catus")]
- , [(321,"anax imperator"), (213,"glaucus atlanticus")]
- }
- instance mod Int where
- (mod) x y
- | sign x == ~ (sign y) && r <> 0 = r + y
- | otherwise = r
- where
- r = x - y * (x / y)
- getASCII :: [Char] -> Int
- getASCII [head:tail] = (toInt head) + getASCII tail
- getASCII [] = 0
- makeList :: String -> [Char]
- makeList str = [x \\ x <-: str]
- //derive gHash TType
- generic gHash a :: a -> Hash
- gHash{|OBJECT of o|} f (OBJECT value) = 3 * (f value) + 5 * (getASCII (makeList o.gtd_name))
- gHash{|CONS of c|} f (CONS value) = 7 * (f value) + 11 * (getASCII (makeList c.gcd_name))
- gHash{|PAIR|} f1 f2 (PAIR value1 value2) = 13 * (f1 value1) + 17 * (f2 value2)
- gHash{|EITHER|} fl fr (LEFT value) = fl value
- gHash{|EITHER|} fl fr (RIGHT value) = fr value
- gHash{|UNIT|} UNIT = 29
- gHash{|Int|} value = 31 * value
- gHash{|String|} value = 37 * (getASCII (makeList value))
- //gHash{|TType|} f f2 (T1 a b End) = (f a) + (f b)
- //gHash{|TType|} f f2 (T2 a b End) = (f a) + (f b)
- //gHash{|TType|} f1 f2 (T1 a b End) = 0
- test_gHash :: [Bool]
- test_gHash =
- [ gHash {|*|} 1 == 31
- , gHash {|*|} 123 == 3813
- , gHash {|*|} "macska" == 23088
- // , gHash {|*|} (T1 1 2 End) == 0
- , gHash {|*|} (T1 1 2 (T2 "macska" "csirke" (T2 "narancs" "alma" (T1 3 4 End)))) == 6466790788407105856
- ]
- makeArray :: Int -> {[(k,v)]}
- makeArray n = {x \\ x <- (getList n [])}
- getList :: Int [[(k,v)]] -> [[(k,v)]]
- getList 0 list = list
- getList n [] = getList (n - 1) [[]]
- getList n [head:tail] = getList (n - 1) [head,[]:tail]
- ht_empty :: Int -> HT k v
- ht_empty n = HT (makeArray n)
- test_ht_empty :: [Bool]
- test_ht_empty =
- [ ht_empty 3 === emptyHT3
- , ht_empty 5 === emptyHT5
- ]
- where
- emptyHT3 :: HT Int Int
- emptyHT3 = HT {[],[],[]}
- emptyHT5 :: HT String String
- emptyHT5 = HT {[],[],[],[],[]}
- Start = test_gHash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement