从包含 2 个以上元素的元组列表中检索元素 (Haskell)

发布于 2024-11-18 10:59:08 字数 757 浏览 7 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃→可爱长大的 2024-11-25 10:59:08

除了 Greg 的出色回答之外,我想指出的是,您不应该将元组用于构成逻辑单元的更大的值集。我建议使用 Account 类型,例如使用记录语法,这使得访问元素或使帐户更改等操作更加方便:

data Account = Account { acNo :: AcNo
                       , name :: Name
                       , city :: City
                       , amount :: Amnt
                       } deriving (Eq, Show)   

请参阅http://learnyouahaskell.com/making-our-own-types-and-typeclasses#record-syntax 了解详细信息。

那么您应该根据 Account 编写函数,而不是根据 AcInfo 编写函数,并使用普通的列表函数。通常,记录提供的提取器功能就足够好了,如您的示例所示:

checkBalance :: [Account] -> AcNo -> Maybe Amnt
checkBalance dbase number = fmap amount $ find (\acc -> number == acNo acc) dbase

这里 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:

data Account = Account { acNo :: AcNo
                       , name :: Name
                       , city :: City
                       , amount :: Amnt
                       } deriving (Eq, Show)   

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 of AcInfo, and use normal list functions. Often the extractor functions provided by the record are good enough, as in your example:

checkBalance :: [Account] -> AcNo -> Maybe Amnt
checkBalance dbase number = fmap amount $ find (\acc -> number == acNo acc) dbase

Here acNo acc gets the account number and amount acc gets the amount from an account.

月亮邮递员 2024-11-25 10:59:08

回想一下,Haskell 类型的名称以大写字母开头,因此 checkBalance 的类型应该是

checkBalance :: AcInfo -> AcNo -> Amnt

在您的问题中,您似乎旨在使用列表理解,但您的语法不太正确。

checkBalance dbase number = head [amnt | (acNo, name, city, amnt) <- dbase,
                                         acNo == number]

如果帐户位于 dbase 中,则此定义没问题,

*Main> checkBalance bankAccounts "oo1"
100

但如果不在 dbase 中,则此定义会崩溃。

*Main> checkBalance bankAccounts "001"
*** Exception: Prelude.head: empty list

checkBalance 的更好类型是

checkBalance :: AcInfo -> AcNo -> Maybe Amnt

代表一般情况,dbase 可能包含也可能不包含number

Recall that names of Haskell types begin with capital letters, so the type of checkBalance should be

checkBalance :: AcInfo -> AcNo -> Amnt

In your question, you seem to aim at using a list comprehension, but you don't have the syntax quite right.

checkBalance dbase number = head [amnt | (acNo, name, city, amnt) <- dbase,
                                         acNo == number]

This definition is fine if an account is in dbase

*Main> checkBalance bankAccounts "oo1"
100

but blows up when it isn't.

*Main> checkBalance bankAccounts "001"
*** Exception: Prelude.head: empty list

A better type for checkBalance is

checkBalance :: AcInfo -> AcNo -> Maybe Amnt

to represent the general case, i.e., dbase may or may not contain number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文