1

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

  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 解析错误时出错这个错误是什么意思?以及如何纠正它?

4

2 回答 2

3

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

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

而不是entering :: Int String [String]-> Book——但这是一个类型错误,而不是一个解析错误。

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

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

于 2012-07-16T07:26:34.487 回答
3

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

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]只会匹配具有单个元素的列表,这可能不是您想要的。完成此功能,或将其注释掉并继续其余部分。显然你目前根本不使用它。等等。

于 2012-07-16T07:50:18.393 回答