0

I'm going through Learn You A Haskell. I have a following function:

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)

which works perfectly fine. I'm now making a list comprehension, where a desired result is something like this:

[(68, "underweight"),(69,"ok"),(70,"ok")]

this is my ghci input:

[(x,y)| x <-[68..70], y <- bmiTell x 185]

and the output is

[(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')]

I tried making it a (x,[y]), but I get the same result with Chars in ""s instead of single quotes

3
  • 1
    Just use [ (x, bmiTell x 185) | x <- [68..70] ] Commented Aug 14, 2016 at 11:19
  • Indeed, thank you, may I have your answer please? so embarassed Commented Aug 14, 2016 at 11:23
  • Put bmiTell X 185 in a list [bmiTell X 185] and your's should work. Remember, strings are lists. The comprehension was pull}ng from the only list it found. Also, two generators in a comprehension are aXb or Cartesian Commented Mar 10, 2018 at 1:50

1 Answer 1

3

You can simply use the map function:

map (\w -> (w, bmiTell w 185)) [68..70]

Or as @ErikR mentioned, using list comprehension:

[ (x, bmiTell x 185) | x <- [68..70] ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.