【发布时间】:2016-08-14 19:33:32
【问题描述】:
我正在学习 Learn You A 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,"ok"),(70,"ok")]
这是我的 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 中的字符而不是单引号得到相同的结果
【问题讨论】:
-
只要使用
[ (x, bmiTell x 185) | x <- [68..70] ] -
确实,谢谢,请问可以给我答案吗? 好尴尬
-
将 bmiTell X 185 放入列表 [bmiTell X 185] 中,你的应该可以工作。请记住,字符串是列表。理解是从它找到的唯一列表中提取的。此外,推导式中的两个生成器是 aXb 或笛卡尔
标签: haskell list-comprehension