Advertisement
csaki

clean 2. zh

Dec 4th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module hash
  2.  
  3. import StdEnv, StdLib, GenEq
  4.  
  5. derive gEq HT, TType
  6.  
  7. :: Hash :== Int
  8.  
  9. :: HT k v = HT {[(k,v)]}
  10.  
  11. :: TType a b = T1 a a (TType a b)
  12.              | T2 b b (TType a b)
  13.              | End
  14.  
  15. test_HT_1 :: HT Int String
  16. test_HT_1 =
  17.       HT { [(0,"Caspian"), (-10,"Joe Satriani")]
  18.          , [(1,"Yngwie Malmsteen"), (6,"Motley Crue")]
  19.          , [(-8,"Black Sabbath"), (2,"Iron Maiden")]
  20.          , [(123,"Skindred"), (3,"Sabaton"), (-7,"Nine Inch Nails")]
  21.          , [(4,"Dire Straits")]
  22.          }
  23.  
  24. test_HT_2 :: HT String (Int,String)
  25. test_HT_2 =
  26.   HT { [("",(2,"Dire Straits"))]
  27.      , [("cat",(10,"Skindred")), ("cowsay",(5,"Joe Satriani"))]
  28.      , [("cp",(9,"Sabaton"))]
  29.      , [("ps aux",(3,"Iron Maiden")), ("wget",(1,"Motley Crue"))]
  30.      , []
  31.      , [("rm",(7,"Nine Inch Nails")), ("ls",(6,"Black Sabbath"))]
  32.      , [("mv",(8,"Caspian")), ("top",(4,"Yngwie Malmsteen"))]
  33.      }
  34.  
  35. test_HT_3 :: HT Int String
  36. test_HT_3 =
  37.   HT { [(122,"felis silvestris catus")]
  38.      , [(321,"anax imperator"), (213,"glaucus atlanticus")]
  39.      }
  40.  
  41. instance mod Int where
  42.   (mod) x y
  43.     | sign x == ~ (sign y) && r <> 0 = r + y
  44.     | otherwise                      = r
  45.     where
  46.       r = x - y * (x / y)
  47.  
  48.  
  49. getASCII :: [Char] -> Int
  50. getASCII [head:tail]        = (toInt head) + getASCII tail
  51. getASCII []                 = 0
  52.  
  53. makeList :: String -> [Char]
  54. makeList str        = [x \\ x <-: str]
  55.  
  56. //derive gHash TType
  57. generic gHash a :: a -> Hash
  58.  
  59. gHash{|OBJECT of o|} f (OBJECT value)           = 3 * (f value) + 5 * (getASCII (makeList o.gtd_name))
  60. gHash{|CONS of c|} f (CONS value)               = 7 * (f value) + 11 * (getASCII (makeList c.gcd_name))
  61. gHash{|PAIR|} f1 f2 (PAIR value1 value2)        = 13 * (f1 value1) + 17 * (f2 value2)
  62. gHash{|EITHER|} fl fr (LEFT value)              = fl value
  63. gHash{|EITHER|} fl fr (RIGHT value)             = fr value
  64. gHash{|UNIT|} UNIT                              = 29
  65. gHash{|Int|} value                              = 31 * value
  66. gHash{|String|} value                           = 37 * (getASCII (makeList value))
  67. //gHash{|TType|} f f2 (T1 a b End)              = (f a) + (f b)
  68. //gHash{|TType|} f f2 (T2 a b End)              = (f a) + (f b)
  69. //gHash{|TType|} f1 f2 (T1 a b End)             = 0
  70.  
  71. test_gHash :: [Bool]
  72. test_gHash =
  73.   [ gHash {|*|} 1        == 31
  74.   , gHash {|*|} 123      == 3813
  75.   , gHash {|*|} "macska" == 23088
  76. //  , gHash {|*|} (T1 1 2 End)  == 0
  77.   , gHash {|*|} (T1 1 2 (T2 "macska" "csirke" (T2 "narancs" "alma" (T1 3 4 End)))) == 6466790788407105856
  78.   ]
  79.  
  80. makeArray :: Int  -> {[(k,v)]}
  81. makeArray n     = {x \\ x <- (getList n [])}
  82.  
  83. getList :: Int [[(k,v)]] -> [[(k,v)]]
  84. getList 0 list          = list
  85. getList n []            = getList (n - 1) [[]]
  86. getList n [head:tail]   = getList (n - 1) [head,[]:tail]
  87.  
  88.  
  89. ht_empty :: Int -> HT k v
  90. ht_empty n      = HT (makeArray n)
  91.  
  92. test_ht_empty :: [Bool]
  93. test_ht_empty =
  94.   [ ht_empty 3 === emptyHT3
  95.   , ht_empty 5 === emptyHT5
  96.   ]
  97.   where
  98.     emptyHT3 :: HT Int Int
  99.     emptyHT3 = HT {[],[],[]}
  100.  
  101.     emptyHT5 :: HT String String
  102.     emptyHT5 = HT {[],[],[],[],[]}
  103.  
  104.  
  105. Start = test_gHash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement