code_junkie

Homework: Doing a binary search on some elements in Haskell

Nov 12th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. data Entry = Entry (String, String)
  2.  
  3. class Lexico a where
  4. (<!), (=!), (>!) :: a -> a -> Bool
  5.  
  6. instance Lexico Entry where
  7. Entry (a,_) <! Entry (b,_) = a < b
  8. Entry (a,_) =! Entry (b,_) = a == b
  9. Entry (a,_) >! Entry (b,_) = a > b
  10.  
  11. entries :: [(String, String)]
  12. entries = [("saves", "en vaut"), ("time", "temps"), ("in", "<`a>"),
  13. ("{", "{"), ("A", "Un"), ("}", "}"), ("stitch", "point"),
  14. ("nine.", "cent."), ("Zazie", "Zazie")]
  15.  
  16. build :: (String, String) -> Entry
  17. build (a, b) = Entry (a, b)
  18.  
  19. diction :: [Entry]
  20. diction = quiksrt (map build entries)
  21.  
  22. size :: [a] -> Integer
  23. size [] = 0
  24. size (x:xs) = 1+ size xs
  25.  
  26. quiksrt :: Lexico a => [a] -> [a]
  27. quiksrt [] = []
  28. quiksrt (x:xs)
  29. |(size [y|y <- xs, y =! x]) > 0 = error "Duplicates not allowed."
  30. |otherwise = quiksrt [y|y <- xs, y <! x]++ [x] ++ quiksrt [y|y <- xs, y >! x]
  31.  
  32.  
  33. english :: String
  34. english = "A stitch in time save nine."
  35.  
  36. show :: Entry -> String
  37. show (Entry (a, b)) = "(" ++ Prelude.show a ++ ", " ++ Prelude.show b ++ ")"
  38.  
  39. showAll :: [Entry] -> String
  40. showAll [] = []
  41. showAll (x:xs) = Main.show x ++ "n" ++ showAll xs
  42.  
  43. main :: IO ()
  44. main = do putStr (showAll ( diction ))
  45.  
  46. function binsearch (x : integer) : integer
  47. local j, k, h : integer
  48. j,k := 1,n
  49. do j+1 <> k --->
  50. h := (j+k) div 2
  51. {a[j] <= x < a[k]} // loop invariant
  52. if x < a[h] ---> k := h
  53. | x >= a[h] ---> j := h
  54. fi
  55. od
  56. {a[j] <= x < a[j+1]} // termination assertion
  57. found := x = a[j]
  58. if found ---> return j
  59. | not found ---> return 0
  60. fi
  61.  
  62. binsearch :: String -> Integer -> Integer -> Entry
  63.  
  64. *Main> map bs (words english)
  65. [Entry ("A","Un"),Entry ("stitch","point"),Entry ("in","<`a>"),Entry ("time","te
  66. mps"),*** Exception: Not found
  67. *Main> map bs (words englishFixed)
  68. [Entry ("A","Un"),Entry ("stitch","point"),Entry ("in","<`a>"),Entry ("time","te
  69. mps"),Entry ("saves","en vaut"),Entry ("nine.","cent.")]
  70. *Main>
  71.  
  72. (!!) :: [a] -> Integer -> a
  73. -- xs!!n returns the nth element of xs, starting at the left and
  74. -- counting from 0.
  75.  
  76. module Main where
  77.  
  78. class Lex a where
  79. (<!), (=!), (>!) :: a -> a -> Bool
  80.  
  81. data Entry = Entry String String
  82.  
  83. instance Lex Entry where
  84. (Entry a _) <! (Entry b _) = a < b
  85. (Entry a _) =! (Entry b _) = a == b
  86. (Entry a _) >! (Entry b _) = a > b
  87. -- at this point, three binary (infix) operators on values of type 'Entry'
  88. -- have been defined
  89.  
  90. type Raw = (String, String)
  91.  
  92. raw_data :: [Raw]
  93. raw_data = [("than a", "qu'un"), ("saves", "en vaut"), ("time", "temps"),
  94. ("in", "<`a>"), ("worse", "pire"), ("{", "{"), ("A", "Un"),
  95. ("}", "}"), ("stitch", "point"), ("crime;", "crime,"),
  96. ("a", "une"), ("nine.", "cent."), ("It's", "C'est"),
  97. ("Zazie", "Zazie"), ("cat", "chat"), ("it's", "c'est"),
  98. ("raisin", "raisin sec"), ("mistake.", "faute."),
  99. ("blueberry", "myrtille"), ("luck", "chance"),
  100. ("bad", "mauvais")]
  101.  
  102. cook :: Raw -> Entry
  103. cook (x, y) = Entry x y
  104.  
  105. a :: [Entry]
  106. a = map cook raw_data
  107.  
  108. quicksort :: Lex a => [a] -> [a]
  109. quicksort [] = []
  110. quicksort (x:xs) = quicksort (filter (<! x) xs) ++ [x] ++ quicksort (filter (=! x) xs) ++ quicksort (filter (>! x) xs)
  111.  
  112. getfirst :: Entry -> String
  113. getfirst (Entry x y) = x
  114.  
  115. getsecond :: Entry -> String
  116. getsecond (Entry x y) = y
  117.  
  118. binarysearch :: String -> [Entry] -> Int -> Int -> String
  119. binarysearch s e low high
  120. | low > high = " NOT fOUND "
  121. | getfirst ((e)!!(mid)) > s = binarysearch s (e) low (mid-1)
  122. | getfirst ((e)!!(mid)) < s = binarysearch s (e) (mid+1) high
  123. | otherwise = getsecond ((e)!!(mid))
  124. where mid = (div (low+high) 2)
  125.  
  126. translator :: [String] -> [Entry] -> [String]
  127. translator [] y = []
  128. translator (x:xs) y = (binarysearch x y 0 ((length y)-1):translator xs y)
  129.  
  130. english :: String
  131. english = "A stitch in time saves nine."
  132.  
  133. compute :: String -> [Entry] -> String
  134. compute x y = unwords(translator (words (x)) y)
  135.  
  136. main = do
  137. putStr (compute english (quicksort a))
Add Comment
Please, Sign In to add comment