【问题标题】:searching in sorted Indexes of tuple list by bubble sort in haskell在haskell中通过冒泡排序搜索元组列表的排序索引
【发布时间】:2012-07-16 07:07:55
【问题描述】:

我选择二进制搜索记录数组的索引作为我用 c++ 和 haskell 进行的研究生研究我编写 c++ 代码和工作,现在我为 haskell one 工作

  import Data.List

  data BookInfo = Book Int String [String]
            deriving (Show)

--输入变量

  entering :: Int String [String]-> Book
  entering id name subject= Book id name subject

--创建元组数组的索引

  index :: [Book]->[Int]
  index [m] = getID (Book id _     _      ) = id
  main :: IO ()
  main = do
  putStrLn "Please enter your book id,name,Subject"
  Book inpStr <- getLine
  putStrLn print r

-- 使用地图的冒泡排序

 bubbleSort ::(Ord x) => [x] -> [x]
 bubbleSort (x':xs) = if x>x' then x': bubbleSort(x:xs)
                  else
                  if x<x' then x: bubbleSort(x':xs)
                  else 
                               x: bubbleSort(X':xs)

 bubble ::[a] -> [a]
 bubble [a] = map bubbleSort [a]

--为数组建立索引

 indexsort(ord a)::[int]->[Int]
 indexsort a=bubble a

--制作元组列表

 addBooks2List Book->[Book]->[Book]
 addBooks2List b m=b:entering b':m

--二进制搜索

 binarysearch [Int]->Int->Int->Int->Int
 a=bubble x
 binaryseach a i m j=
 let u=(i+m)/2
 if a[u]=j then u
 if a[u]>j then binaryseach a i u-1 j
 else 
 if a[u]<j then binarysearch a u+1 m j
 if i=m "Not found"

--打印具有搜索到的 id 的元组

 print::Int->Book
 print r= Book r y z 
 r=binaryseach m 0 (length m)

它在输入“进入”时在 8:3 解析错误时出错,这个错误是什么意思?以及如何纠正?

【问题讨论】:

    标签: haskell tuples


    【解决方案1】:

    一方面,entering 的类型签名应该是

    entering :: Int -> String -> [String] -> BookInfo
    

    而不是entering :: Int String [String]-&gt; Book——但这是类型错误,而不是解析错误。

    也许您的缩进是错误的,但是如果没有逐字提供的代码,这很难说。请记住:在 Haskell 中,与 C 和 Java 等语言不同,代码的布局很重要。

    无论如何,正如您发布的那样,代码远非解决您的问题的有效解决方案。您可能想退后几步,学习如何在 Haskell 中编写基本函数,然后再学习如何将它们粘合在一起以编写更多涉及的程序。我真的怀疑通过尝试将 C++ 的 sn-ps 转换为 Haskell 来学习该语言是否有很高的成功机会。不过只是我的两分钱......

    【讨论】:

      【解决方案2】:

      你的代码有很多问题,你最好从一个函数开始,慢慢地扩展你的程序,看看你得到编译的增量部分。从这个开始:

      entering :: Int String [String]-> Book
      entering id name subject= Book id name subject
      

      首先,Book 不是类型而是数据构造函数,类型是BookInfo。然后,你错过了箭头(正如 dblhelix 指出的那样)。所以它应该是:

      entering :: Int -> String -> [String]-> BookInfo
      entering id name subject= Book id name subject
      

      这将编译。但是,entering 现在与Book 相同。无论如何,继续添加下一个函数并编译它等等。

      如果我们继续,

      index :: [Book]->[Int]
      index [m] =
      

      缺少定义(= 右侧的内容是什么?),[m] 只会匹配具有单个元素的列表,这可能不是您想要的。完成此功能,或将其注释掉并继续其余部分。显然你目前根本不使用它。以此类推。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        • 2013-03-19
        • 1970-01-01
        • 2016-05-17
        • 2013-10-09
        • 2016-02-10
        • 1970-01-01
        相关资源
        最近更新 更多