haskell - 从列表中提取Haskell中的单个整数

标签 haskell

对于 Haskell 来说是全新的。问题 - 我有一个类型构造函数,其值构造函数由 4 个组件组成:

 TrackPoint :: TP { rpm :: Integer
               , time :: Integer
               , distance :: Float
               , speed :: Float 
               } deriving (Show)

我想要 [TrackPoint] 并让它在转速值低于 10,000 时返回时间、距离和速度。我尝试过使用守卫,但没有成功。这位新手将不胜感激任何帮助。

最佳答案

简单的功能:

processTrackPoints :: [TrackPoint] -> [(Integer, Float, Float)]
processTrackPoints tps = 
  map (\tp -> (time tp, distance tp, speed tp)) $
  filter (\tp -> rpm tp > 10000) tps

相同,但尽可能无积分:

processTrackPoints :: [TrackPoint] -> [(Integer, Float, Float)]
processTrackPoints = 
  map (\tp -> (time tp, distance tp, speed tp)) .
  filter ((> 10000) . rpm)

使用守卫:

processTrackPoints :: [TrackPoint] -> [(Integer, Float, Float)]
processTrackPoints ((TP rpm time distance speed) : t)
  | rpm > 10000 = (time, distance, speed) : processTrackPoints t
  | otherwise = processTrackPoints t
processTrackPoints _ = []

当然,这就是全部,假设您已经正确定义了数据类型,如下所示:

data TrackPoint = 
  TP { 
    rpm :: Integer, 
    time :: Integer, 
    distance :: Float, 
    speed :: Float 
  } 
  deriving (Show)

关于haskell - 从列表中提取Haskell中的单个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16265512/

相关文章:

model-view-controller - Haskell Web 框架的最新技术是什么

haskell - 类型类声明中 "type"声明的含义

haskell - Haskell 中的求幂

haskell - 如何通过堆栈获取 GHC Core 输出?

haskell - 如何在Haskell中释放一个可以在另一个程序中使用的模块?

haskell - 为什么从根本上说遍历是在 Applicatives 上定义的?

string - 用列表中的字符串替换字符串

haskell - 为 GHC 指定源目录的问题

java - 从 Java 中调用 Haskell 函数的最佳方式

haskell - GHCi 可以告诉我本地 Haskell 函数的类型吗?