- ShoulderDaemon
- Oct 9, 2003
-
support goon fund
-
Taco Defender
|
I know that defining veriables inside the then clause of an if "statement" is a terrible and un-Haskell-y thing to do, but I don't know how else to accomplish what I'm trying to do. I have a great and effective Haskell expression that gets me a list of two elements. I need the different of those two elements. I want to assign the difference of the value at index 1 and the value at index 0 to the then clause. I cannot figure out how to do it.
Either lift the where out of the if:
code:foo f = if <some basic condition>
then Just (list!!1 - list!!0)
else Nothing
where list = <long expression that generates the list>
or use a let expression:
code:foo f = if <some basic condition>
then let list = <long expression that generates the list>
in Just (list!!1 - list!!0)
else Nothing
Because of lazy evaluation, these are both exactly the same (the list will not be created unless it is actually required). They're also both approximately equivalent in terms of "looking like normal Haskell" and if you read other people's code you'll see lots of both. Pick whichever one you prefer.
|
#
¿
Sep 29, 2015 00:26
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 2, 2024 00:04
|
|