Reputation: 13
i'm newbie in haskell language. i wrote function get list of pairs as input and return list of BMI index:
calcBMI:: (RealFloat a)=> [(a,a)]->[a]
calcBMI xs = [ result | (w,h)<-xs, let bmifunc (w,h)= w/h^2; result =bmifunc (w,h) in result >=25]
when i save and :reload in ghci, error: Not in scope: `result' Failed, modules loaded: none appear. i think the list comprehension don't know what is resultwhich i have introduced in predicate of list comprehension. Please tell me why? and how to fix the problem
Upvotes: 0
Views: 64
Reputation: 916
Any variables defined in let ... in
is only effective in the expression after in
. So, you cannot use your result
in anywhere else. Luckily Haskell list comprehension expression allows you to define variables in it that is referrable anywhere in the expression, by simply adding a let x = ...
generator. Hence instead of writing
calcBMI xs =
[result | (w, h) <- xs,
let bmifunc (w, h) = w / h ^ 2;
result = bmifunc (w, h)
in result >= 25]
You can write
calcBMI xs =
[result | (w, h) <- xs,
let bmifunc (w, h) = w / h ^ 2;
result = bmifunc (w, h),
result >= 25]
and the code will compile.
Upvotes: 0