我正在学习“学习Haskell”。我有以下功能:
bmiTell :: (RealFloat a) => a -> a-> String
bmiTell weight height
| bmi <= skinny ="underweight"
| bmi <= normal = "ok"
| bmi <= fat = "fat"
| otherwise = "whale"
where
bmi = weight/height^2
(skinny,normal,fat)=(18.5, 25.0,30.0)
效果很好。我现在要进行列表理解,所需的结果是这样的:
[(68,“体重不足”),(69,“确定”),(70,“确定”)]
这是我的ghci输入:
[(x,y)| x <-[68..70], y <- bmiTell x 185]
和输出是
[(68.0,'u'),(68.0,'n'),(68.0,'d'),(68.0,'e'),(68.0,'r'),(68.0,'w'),( 68.0,'e'),(68.0,'i'),(68.0,'g'),(68.0,'h'),(68.0,'t'),(69.0,'u'),(69.0, 'n'),(69.0,'d'),(69.0,'e'),(69.0,'r'),(69.0,'w'),(69.0,'e'),(69.0,'i '),(69.0,'g'),(69.0,'h'),(69.0,'t'),(70.0,'u'),(70.0,'n'),(70.0,'d') ,(70.0,'e'),(70.0,'r'),(70.0,'w'),(70.0,'e'),(70.0,'i'),(70.0,'g'),( 70.0,'h'),(70.0,'t')]
我尝试将其设置为(x,[y]),但使用“” s中的Chars而不是单引号得到相同的结果
您可以简单地使用以下map
功能:
map (\w -> (w, bmiTell w 185)) [68..70]
或如@ErikR所述,使用列表理解:
[ (x, bmiTell x 185) | x <- [68..70] ]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句