Haskell返回有多少输入大于其平均值 [英] Haskell returns how many inputs are larger than their average value
问题描述
我对haskell很陌生,编写了一个简单的代码来返回多少输入大于它们的平均值。我得到错误:
$ b
错误文件:.\AverageThree.hs:5 - 在应用程序中输入错误
*表达式:xyz
术语:x
类型:Int
* 不匹配:a - > b - > c
代码:
averageThree :: Int - > Int - > Int - > Float
averageThree x y z =(fromInteral x + fromInteral y + fromIntegral z)/ 3
howManyAverageThree :: Int - > Int - > Int - > Int
howManyAverageThree x y z = length> averageThree
任何人都可以帮助我?
首先,你不是应用函数 length
或 averageThree
- 因此也不会将您的参数用于 howManyAverageThree
。 其次,长度
的类型是 [a ] - > INT
。由于您在这里没有列表,您必须使用不同的函数,或者创建一个列表。
如果我正确理解了您所需的算法,那么您要需要做一些事情:
- 应用
x
y
和z
至averageThree
。 - 使用
过滤器
功能,将此计算的平均值与每个传入的参数进行比较;这将产生一个列表。 - 查找结果列表的长度。
代码我冲了出来做到这一点:
howManyAverageThree :: Int - > Int - > Int - > Int
函数,我在上面提到过。
howManyAverageThree xyz = length $ filter(> avg)the_three
其中avg = averageThree xyz
the_three = [fromIntegral x,fromIntegral y,fromInteral z]
$ c
$ b这充分利用了一些简洁的功能:
$
。此运算符只是将函数应用程序从左关联变为右关联。
I'm very new to haskell, writing a simple code that returns how many inputs are larger than their average value. I got error:
ERROR file:.\AverageThree.hs:5 - Type error in application * Expression : x y z Term : x Type : Int * Does not match : a -> b -> c
Code:
averageThree :: Int -> Int -> Int -> Float averageThree x y z = (fromIntegral x+ fromIntegral y+ fromIntegral z)/3 howManyAverageThree ::Int -> Int -> Int -> Int howManyAverageThree x y z = length > averageThree
Anyone help me?
The trouble you're having comes from a few places.
First, you aren't applying either function, length
or averageThree
- and hence also not using your arguments to howManyAverageThree
.
Second, the type of length
is [a] -> Int
. As you don't have a list here, you either have to use a different function, or make a list.
If I understand your desired algorithm correctly, you are going to need to do a few things:
- Apply
x
y
andz
toaverageThree
. - Use the
filter
function, comparing this computed average with each passed in parameter; this will result in a list. - Find the length of the resulting list.
The code I dashed off to do this follows:
howManyAverageThree ::Int -> Int -> Int -> Int
howManyAverageThree x y z = length $ filter (> avg) the_three
where avg = averageThree x y z
the_three = [fromIntegral x,fromIntegral y,fromIntegral z]
This takes advantage of a couple of neat features:
- Currying, sometimes called "partial function application". That's what I was using with (> avg); normally, the infix function
>
takes two parameters of the same type, and returns a Bool - by wrapping in parenthesis and providing an expression on one side, I have partially applied it, which allows it to be used as a filter function - The
where
keyword. I used this to clean it all up a little and make it more readable. - The
filter
function, which I mentioned above. - Function application using
$
. This operator just changes the function application from left-associative to right-associative.
这篇关于Haskell返回有多少输入大于其平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!