views:

149

answers:

3

If i wanted to do the following:

function1 stuff
| condition1 = "yay" 
| condition2 = "hey" 
| condition3 = "nay" 

and i wanted to have a statement that always ran, unconditionally, (e.g. "because"), how could i do this? The strings are placeholders.

Thanks!

Edit:

Before i give up on this, i want to try and rephrase it in terms of the problem at hand.

I have a function which takes two inputs (lists of lists).

If given a sample input of [[]] [[[]], [[]]] (or [[]] [[["x"]], [[]]]) i want to

  • test to see if there exists an even amount of [[]]'s on each side and if so delete them all
  • test for an odd amount on each side then nub (delete all but one)
  • several other similar things

then following this combine the two inputs (cleaned up) and return them. So far i had decided on a guard syntax, after trying case and if-then-else. But that would only allow me to test one side (for example) then after the = sign to join and return. Unless it was inordinately long... So acknowledging that i have a lot to learn yet, how would i go about doing something like this in haskell?

+1  A: 

If you mean a condition that succeeds if all the others fail, then just use otherwise:

foo x
    | x == 0 = "yay"
    | x == 1 = "hey"
    | x >= 2 = "nay"
    | otherwise = "negative"

If you mean something else by "always ran", i.e. it would "run" even if another one also runs, then I think you are thinking wrong for this language. For example, if we had the syntax:

foo x
    | x == 1 = "one"
    | always = "any"

Then what should be the value of foo 1 ?

luqui
True, i still haven't managed to get my head around haskell thinking. But i am at a point in a project where i have a function that is almost right - but would need a statement to be run after the guards do their job. I guess i would have to make another function or figure out a way to include it in the "yay/hey" statement, ugly as that would be (and long...).
commentator8
And re your second example, i was intending the like of foo x, x = 2 * x, | x == 1 = "one",however bad that looks (commas signifying line break)...
commentator8
+2  A: 

The only purpose of a function is to return a value. If you, however, have some commonality that each condition shares you can use where

function1 stuff
  | condition1 = "yay" ++ otherstuff
  | condition2 = "hey" ++ otherstuff
  | condition3 = "nay" ++ otherstuff
  where otherstuff = "!"
cobbal
+4  A: 

Your questions don't really apply to functional programming languages, such as Haskell. Your first question is, I think, "how do I always do A here when I also want to do B?". Your second question is similar: "how do I do A then B?". These questions make sense for imperative languages (such as OO languages) where your program essentially tells the computer how to proceed. In functional languages, however, you say what you want computed, but it's entirely up to the compiler how this is done (take that with a pinch of salt, but that's the general idea).

For example, your second question has

number = number * 5
number = number * 2

In an imperative language this reads "update the memory cell named 'number' with its current contents times 5; then update the the memory cell named 'number' with its current contents times 2".

In a functional programming language these statements are either an error (you're trying to define a scalar quantity "number" in terms of itself) or are Boolean expressions ("is number equal to number * 5?").

The best advice I can offer to help clear up your confusion is this: forget all about the idea of updating variables in a functional programming language because you can't do it. Instead, think of always computing the value you want. For example, here's the list length function:

length []     = 0                 -- The empty list has zero items.
length (x:xs) = 1 + length xs     -- The non-empty list (x:xs) has one more item than xs.

Hope this helps.

Rafe
As has been pointed out - im oh so slowly getting my head around the thinking required for haskell. But i guess that only comes with practice and time.And yes that did help!
commentator8