If i wanted to do the following:
function1 stuff
| condition1 = "yay"
| condition2 = "hey"
| condition3 = "nay"
and i wanted to have a statement that always ra开发者_开发问答n, 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?
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.
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
?
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 = "!"
精彩评论