Haskell:比较元组列表中的元素
Haskell: comparing elements in a tuple list
项目说明:
给定一个包含 5 个元素元组的列表(例如 [(String,Int,String,Int,Int)]
),元组的第一个元素代表工人的姓名,第四个元素代表 his/her 薪水,我必须创建一个函数(称为 biggestPay
),给出薪水最高的工人的姓名。
限制:
按照这本书的要求"Learn you a Haskell for a great good"我只能使用(包括)高阶函数和 Prelude 函数。
我目前的作品:
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.
x is ...
是无效的 Haskell 语法,但您可以像这样使用 let
:
getPayList (x:xs) = [ x | y <- [...]
, let x = if ... then ... else ... ]
但是,您的方法还有很多其他问题。例如,这个代码片段:
getPay(getPayList y:xs)
被Haskell解释为
getPay( (getPayList y) : xs)
不进行类型检查,因为 y 是一个整数,而 getPayList
对元组列表进行操作。
提示
看看LYAH如何引入maximum函数怎么样:
http://learnyouahaskell.com/recursion
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
| x > maxTail = x -- change this line
| otherwise = maxTail
where maxTail = maximum' xs
也许对该函数进行简单的调整,即可获得 biggestPay
函数。
项目说明:
给定一个包含 5 个元素元组的列表(例如 [(String,Int,String,Int,Int)]
),元组的第一个元素代表工人的姓名,第四个元素代表 his/her 薪水,我必须创建一个函数(称为 biggestPay
),给出薪水最高的工人的姓名。
限制:
按照这本书的要求"Learn you a Haskell for a great good"我只能使用(包括)高阶函数和 Prelude 函数。
我目前的作品:
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.
x is ...
是无效的 Haskell 语法,但您可以像这样使用 let
:
getPayList (x:xs) = [ x | y <- [...]
, let x = if ... then ... else ... ]
但是,您的方法还有很多其他问题。例如,这个代码片段:
getPay(getPayList y:xs)
被Haskell解释为
getPay( (getPayList y) : xs)
不进行类型检查,因为 y 是一个整数,而 getPayList
对元组列表进行操作。
提示
看看LYAH如何引入maximum函数怎么样:
http://learnyouahaskell.com/recursion
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
| x > maxTail = x -- change this line
| otherwise = maxTail
where maxTail = maximum' xs
也许对该函数进行简单的调整,即可获得 biggestPay
函数。