【发布时间】:2015-10-26 11:48:38
【问题描述】:
项目说明:
给定一个包含 5 个元素元组的列表(例如 [(String,Int,String,Int,Int)]),元组的第一个元素代表工人的姓名,第四个元素代表他/她的薪水,我必须创建一个函数(称为 @ 987654322@),它给出了薪水最高的工人的名字。
限制:
按照“Learn you a Haskell for a great good”一书,我只能使用(包括)高阶函数和前奏函数的所有内容。
到目前为止我的作品:
getPay :: (String,Int,String,Int,Int) -> Int
getPay (_,_,_,a,_) = a
getName ::(String,Int,String,Int,Int) -> String
getName (a,_,_,_,_) = a
getPayList :: [(String,Int,String,Int,Int)] -> String
getPayList [] = []
getPayList xs = [ x | y <- [0..(length xs)-1] ,
if getPay(getPayList y:xs) >= getPay(getPayList (y+1):xs)
then x is getName(getPayList y:xs)
else x is getName(getPayList (y+1):xs)]
biggestPay :: [String] -> String
biggestPay [] = []
biggestPay xs = drop ((length xs) -1) xs
我的想法是比较所有工人的薪水并将他们的名字存储在一个列表中,然后最后因为列表的最后一个元素是薪水最高的工人,所以我会删除所有其他元素以获得该工人的名字仅限。
但是,当我尝试将此函数加载到 GHCI 中时,出现以下错误:
ghci> :l Pay.hs
[1 of 1] Compiling Main ( Pay.hs, interpreted )
Pay.hs:9:19: Not in scope: `x'
Pay.hs:11:22: Not in scope: `x'
Pat.hs:11:24:
Not in scope: `is'
Perhaps you meant one of these:
`xs' (line 9), `id' (imported from Prelude)
Pay.hs:12:22: Not in scope: `x'
Pay.hs:12:24:
Not in scope: `is'
Perhaps you meant one of these:
`xs' (line 9), `id' (imported from Prelude)
Failed, modules loaded: none.
【问题讨论】:
-
is是什么意思?
标签: list haskell comparison tuples