对Haskell中的一些元素进行二分查找

时间:2008-11-15 23:45:37

标签: search haskell

我正在尝试完成我的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',看起来   英语 - 法语中的每个单词   字典使用二进制搜索,   进行逐字替换,   组装法语翻译,和   打印出来。

     

“快速排序”功能拒绝   重复的条目('错误'/中止)   所以只有一个法国人   任何英文单词的定义。测试   两个原始的'quicksort'   'raw_data'并在添加之后   '(“save”,“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'是,   如果你可以设计这两个功能   在整数上,它是微不足道的   将它们抬起来操作Entry's。

有人知道我应该如何处理我的二元搜索功能吗?

4 个答案:

答案 0 :(得分:4)

教师要求“文字音译”,因此请使用相同的变量名称,顺序相同。但请注意一些差异:

  • 给定版本只需1 参数,他给出的签名 需要3.嗯,
  • 给定的版本不是递归的,但他要求一个 递归版。

另一个答案是转换为数组,但对于这么小的练习(毕竟这是功课),我觉得我们可以假装列表是直接访问。我刚刚接受了你的用词:: [Entry]并将其编入索引。我确实需要在几个地方之间转换Int和Integer。

Minor nit:你的英语价值有误(bs是我做的binSearch的捷径):

  *Main> map bs (words english)
[Entry ("A","Un"),Entry ("stitch","point"),Entry ("in","<`a>"),Entry ("time","te
mps"),*** Exception: Not found
*Main> map bs (words englishFixed)
[Entry ("A","Un"),Entry ("stitch","point"),Entry ("in","<`a>"),Entry ("time","te
mps"),Entry ("saves","en vaut"),Entry ("nine.","cent.")]
*Main>

答案 1 :(得分:3)

二进制搜索需要随机访问,这在列表中是不可能的。因此,要做的第一件事可能是将列表转换为Array(使用listArray),并对其进行搜索。

答案 2 :(得分:1)

这是我的代码,只是问题的英文部分(我测试了它,它完美地运行):

module Main where

class Lex a where
    (<!), (=!), (>!) :: a -> a -> Bool

data Entry = Entry String String

instance Lex Entry where
    (Entry a _) <!  (Entry b _) = a <  b
    (Entry a _) =!  (Entry b _) = a == b
    (Entry a _) >!  (Entry b _) = a >  b
  -- at this point, three binary (infix) operators on values of type 'Entry'
  -- have been defined

type Raw = (String, String)

raw_data :: [Raw]
raw_data  =  [("than a", "qu'un"), ("saves", "en vaut"), ("time", "temps"),
                ("in", "<`a>"), ("worse", "pire"), ("{", "{"), ("A", "Un"),
                ("}", "}"), ("stitch", "point"), ("crime;", "crime,"),
                ("a", "une"), ("nine.", "cent."), ("It's", "C'est"),
                ("Zazie", "Zazie"), ("cat", "chat"), ("it's", "c'est"),
                ("raisin", "raisin sec"), ("mistake.", "faute."),
                ("blueberry", "myrtille"), ("luck", "chance"),
                ("bad", "mauvais")]

cook :: Raw -> Entry
cook (x, y) = Entry x y

a :: [Entry]
a = map cook raw_data

quicksort :: Lex a => [a] -> [a]
quicksort []     = []
quicksort (x:xs) = quicksort (filter (<! x) xs) ++ [x] ++ quicksort (filter (=! x) xs) ++ quicksort (filter (>! x) xs) 

getfirst :: Entry -> String
getfirst (Entry x y) = x

getsecond :: Entry -> String
getsecond (Entry x y) = y

binarysearch :: String -> [Entry] -> Int -> Int -> String
binarysearch s e low high 
    | low > high = " NOT fOUND "
    | getfirst ((e)!!(mid)) > s = binarysearch s (e) low (mid-1)
    | getfirst ((e)!!(mid)) < s = binarysearch s (e) (mid+1) high
    | otherwise = getsecond ((e)!!(mid))
        where mid = (div (low+high) 2)

translator :: [String] -> [Entry] -> [String]
translator [] y = []
translator (x:xs) y = (binarysearch x y 0 ((length y)-1):translator xs y)

english :: String
english = "A stitch in time saves nine."

compute :: String -> [Entry] -> String
compute x y = unwords(translator (words (x)) y)

main = do
    putStr (compute english (quicksort a))

答案 3 :(得分:0)

一个重要的Prelude运算符是:

(!!) :: [a] -> Integer -> a
-- xs!!n returns the nth element of xs, starting at the left and
-- counting from 0.

因此,[14,7,3]!!1 ~~&gt; 7。