Haskell返回有多少输入大于其平均值 [英] Haskell returns how many inputs are larger than their average value

查看:119
本文介绍了Haskell返回有多少输入大于其平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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 。由于您在这里没有列表,您必须使用不同的函数,或者创建一个列表。



如果我正确理解了您所需的算法,那么您要需要做一些事情:


  1. 应用 x y z averageThree

  2. 使用 过滤器功能,将此计算的平均值与每个传入的参数进行比较;这将产生一个列表。

  3. 查找结果列表的长度。

代码我冲了出来做到这一点:

  howManyAverageThree :: Int  - > Int  - > Int  - > Int 
howManyAverageThree xyz = length $ filter(> avg)the_three
其中avg = averageThree xyz
the_three = [fromIntegral x,fromIntegral y,fromInteral z]

$ b

这充分利用了一些简洁的功能:


    ,有时称为部分功能应用程序。这就是我用的(> avg);通常,中缀函数> 接受两个相同类型的参数,并返回一个Bool - 通过包装在圆括号中并提供一个表达式,我已经部分应用它,它允许它用作过滤器函数
  1. 其中关键字。我使用它来清理它,并使其更具可读性。
  2. 过滤器函数,我在上面提到过。

  3. 函数应用程序使用 $ 。此运算符只是将函数应用程序从左关联变为右关联。


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:

  1. Apply x y and z to averageThree.
  2. Use the filter function, comparing this computed average with each passed in parameter; this will result in a list.
  3. 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:

  1. 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
  2. The where keyword. I used this to clean it all up a little and make it more readable.
  3. The filter function, which I mentioned above.
  4. Function application using $. This operator just changes the function application from left-associative to right-associative.

这篇关于Haskell返回有多少输入大于其平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆