【发布时间】:2008-11-15 23:45:36
【问题描述】:
我正在尝试完成我的 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 程序 英文句子“english”,看起来 提高英语-法语中的每个单词 使用二进制搜索的字典, 执行逐字替换, 组装法语翻译,和 打印出来。
函数“快速排序”拒绝 重复条目(带有“错误”/中止) 所以恰好有一个法国人 任何英语单词的定义。测试 '快速排序'与原始 'raw_data' 并在添加后 '("saves", "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'类型 '[Entry]' 是全局可见的。暗示: 把你的字符串(英文单词)变成 进入后立即“进入” 'binsearch'。
的编程价值 高级数据类型“条目”是, 如果你能设计这两个功能 在整数上,它是微不足道的 将它们抬起以在 Entry 上方进行操作。
有人知道我应该如何使用二进制搜索功能吗?
【问题讨论】: