Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Snak posted:

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.

Adbot
ADBOT LOVES YOU

  • Locked thread