从包含 2 个以上元素的元组列表中检索元素 (Haskell)
我是 Haskell 的新手,在这种情况下需要一些帮助。我有以下列表
-- create a type for bank account
type AcNo = String
type Name = String
type City = String
type Amnt = Int
type AcInfo = [(AcNo, Name, City, Amnt)]
-- function to get the data of bank accounts to a list of tuples
bankAccounts :: AcInfo
bankAccounts = [("oo1", "Sahan", "Colomb", 100),("002", "John", "Jafna", 200)]
我的要求是获取与帐号相对应的金额,例如,对于001,它应该给出100。
我编写的函数是这样的
--Function to check the balance of a person
checkBalance :: bankAccounts -> AcNo -> Amnt
checkBalance dbase number = Amnt|(AcNo, Name, City, Amnt) <- dbase, AcNo==number}
错误消息
Syntax error in input (unexpected `|')
第二行是我卡住的地方,它给出了我想要的 对此有一些帮助。谢谢。
I'm new to Haskell and need some help on this situation. I have the following list
-- create a type for bank account
type AcNo = String
type Name = String
type City = String
type Amnt = Int
type AcInfo = [(AcNo, Name, City, Amnt)]
-- function to get the data of bank accounts to a list of tuples
bankAccounts :: AcInfo
bankAccounts = [("oo1", "Sahan", "Colomb", 100),("002", "John", "Jafna", 200)]
My requirement is to get the amount corresponding to the account number, e.g., for 001 it should give 100.
The function I wrote was this
--Function to check the balance of a person
checkBalance :: bankAccounts -> AcNo -> Amnt
checkBalance dbase number = Amnt|(AcNo, Name, City, Amnt) <- dbase, AcNo==number}
The second line is where im stuck at which gives the error message
Syntax error in input (unexpected `|')
I'd like to have some help on this. Thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Greg 的出色回答之外,我想指出的是,您不应该将元组用于构成逻辑单元的更大的值集。我建议使用
Account
类型,例如使用记录语法,这使得访问元素或使帐户更改等操作更加方便:请参阅http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax 了解详细信息。
那么您应该根据
Account
编写函数,而不是根据AcInfo
编写函数,并使用普通的列表函数。通常,记录提供的提取器功能就足够好了,如您的示例所示:这里
acNo acc
获取帐号,amount acc
获取帐户的金额。Additional to Greg's excellent answer I want to point out that you shouldn't use tuples for bigger sets of values that constitute a logical unit. I would suggest to have an
Account
type, e.g. using record syntax, which makes things like accessing elements or making account changes more convenient:See http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax for details.
Then you should write functions in terms of
Account
, not in terms ofAcInfo
, and use normal list functions. Often the extractor functions provided by the record are good enough, as in your example:Here
acNo acc
gets the account number andamount acc
gets the amount from an account.回想一下,Haskell 类型的名称以大写字母开头,因此
checkBalance
的类型应该是在您的问题中,您似乎旨在使用列表理解,但您的语法不太正确。
如果帐户位于
dbase
中,则此定义没问题,但如果不在 dbase 中,则此定义会崩溃。
checkBalance
的更好类型是代表一般情况,即,
dbase
可能包含也可能不包含number
。Recall that names of Haskell types begin with capital letters, so the type of
checkBalance
should beIn your question, you seem to aim at using a list comprehension, but you don't have the syntax quite right.
This definition is fine if an account is in
dbase
but blows up when it isn't.
A better type for
checkBalance
isto represent the general case, i.e.,
dbase
may or may not containnumber
.