I am trying to complete the last part of my Haskell homework, and I am stuck, my code so far:
data Entry = Entry (String, String) class Lexico a where (<!), (=!), (>!) :: a -> a -> Bool instance Lexico Entry where Entry (a,_) <! Entry (b,_) = a < b Entry (a,_) =! Entry (b,_) = a == b Entry (a,_) >! Entry (b,_) = a > b entries :: [(String, String)] entries = [("saves", "en vaut"), ("time", "temps"), ("in", "<`a>"), ("{", "{"), ("A", "Un"), ("}", "}"), ("stitch", "point"), ("nine.", "cent."), ("Zazie", "Zazie")] build :: (String, String) -> Entry build (a, b) = Entry (a, b) diction :: [Entry] diction = quiksrt (map build entries) size :: [a] -> Integer size [] = 0 size (x:xs) = 1+ size xs quiksrt :: Lexico a => [a] -> [a] quiksrt [] = [] quiksrt (x:xs) |(size [y|y <- xs, y =! x]) > 0 = error "Duplicates not allowed." |otherwise = quiksrt [y|y <- xs, y <! x]++ [x] ++ quiksrt [y|y <- xs, y >! x] english :: String english = "A stitch in time save nine." show :: Entry -> String show (Entry (a, b)) = "(" ++ Prelude.show a ++ ", " ++ Prelude.show b ++ ")" showAll :: [Entry] -> String showAll [] = [] showAll (x:xs) = Main.show x ++ "\n" ++ showAll xs main :: IO () main = do putStr (showAll ( diction ))
The question asks:
Write a Haskell program that has an English sentence "English", looks for every word in the English-French dictionary using binary search, performs a phrase, collects a French translation and prints it.
The quicksort function rejects duplicate entries (with error / interruption) so there is exactly one French definition for any English word. Verification work of "quicksort" with the original 'raw_data' and after adding '("saves", "sauve")' to 'raw_data'.
Here is von Neumann, a late-stop version of the binary search. Do literal transliteration in Haskell. Immediately after entering Haskell, the version should check for a recursive "loop invariant" that ends with 'error' / abort if it is not held. It also stops in the same way if the English word is not found.
function binsearch (x : integer) : integer local j, k, h : integer j,k := 1,n do j+1 <> k ---> h := (j+k) div 2 {a[j] <= x < a[k]} // loop invariant if x < a[h] ---> k := h | x >= a[h] ---> j := h fi od {a[j] <= x < a[j+1]} // termination assertion found := x = a[j] if found ---> return j | not found ---> return 0 fi
In the Haskell version
binsearch :: String -> Integer -> Integer -> Entry
how the constant dictionary 'a' of type "[Entry]" is displayed worldwide. Hint: Make your line (English word) in the "Login" immediately after entering 'Binsearch'.
The value of programming the high-level Entry data type is if you can design these two functions on integers trivially lift them to work on the record.
Does anyone know how I should go about my binarysearch function?