我想完成我的家庭作業的Haskell的最後一部分,我卡住了,到目前爲止我的代碼:上做哈斯克爾
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))
問題問:
寫Haskell程序是需要 英文句子「英語」,採用二進制搜索查找 了每個單詞在英語 - 法語字典 , 執行字對字的替換, 組裝神父ench翻譯和 將其打印出來。
功能「快速排序」拒絕 重複的條目(以「錯誤」 /中止) ,以便有一個精確法國 定義爲任何英文單詞。測試 'quicksort'與原始 'raw_data'以及在將 '(「保存」,「sauve」)'添加到'raw_data'之後。
這裏是一個馮諾依曼遲到 版本的二進制搜索。將 直接轉換成Haskell。 立即進入後,Haskell 版本必須驗證遞歸 「循環不變」,如果無法保持,則以 '錯誤'/終止爲終止。如果 英文單詞未找到,它也會以相同的方式終止。
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
在Haskell的版本
binsearch :: String -> Integer -> Integer -> Entry
作爲恆定字典 'A' 型的 '[輸入]' 是全局可見的。提示: 在輸入 'binsearch'後立即將您的字符串(英文單詞)變成 'Entry'。
的 高級數據類型「進入」的節目價值在於, 如果你能在整數設計這兩個函數 ,這是微不足道的 提升他們對工作在入學的。
有人知道我應該如何去關於我的二進制搜索功能?
是的,錯字給了一堆麻煩,當我得到代碼運行時,我得到了一些無限循環。 – Flame 2008-11-16 07:35:41