Using do notation with if/else [closed] - haskell

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to use do notation to only return a list element if it's even:
ghci> do { x <- [1,2,3]; if (even x) [x] else []}
<interactive>:43:36: parse error on input `else'
What am I doing wrong?
In addition, please note if this code is non-idiomatic.

You forgot the "then".
Also, i'm not really sure which Monad is this..
do { x <- [1,2,3]; if (even x) then [x] else []}

Related

I migrated from python 2.7 to 3.8.5, now my code won't run. I am showing UnboundLocalError: local variable 'Url_name' referenced before assignment [duplicate]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Python:
I have researched a lot into this, but as a beginner I don't understand the solutions, let alone apply them to my simple problem:
def min_max(xs):
xs = []
for i in xs:
y = (min, max)
return y
"local variable 'y' referenced before assignment"
I don't understand why this does NOT work!!
I've defined Y in the line before return, i know there is another way to do it, but i really need to know why this method is wrong!
Thanks!
assuming that min and max are defined somewhere else....
The loop for i in xs will go around once for each item in xs. Since xs is empty, the loop will not run at all. So, y is never set.

How can I access the 1st element of a nested list? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a nested list and I want to pull out the first element of every list of lists:
t = [
[['a',1],['b',2],['c',3]],
[['d',1],['e',2],['f',3]],
[['g',1],['h',2],['i',3]]
]
want = ['a','d','g']
I am getting the Comphrension wrong:
list = [x[0][0] for x in t]
It will work as long as you don't call your variable 'list' (call it 'new_t' or whatever) - list is a reserved word in python.

Lexical fault when using ''BOOM!'' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I type this into my texteditor:
boombang xs = [ if x < 10 then ”BOOM!” else ”BANG!”
But when trying to load it, my terminal denies it and and says:
Probably some dumb rookie mistake, but I can't seem to find the solution...:/
There are two problems here.
The first is, you're not using plain ASCII quotation marks (U+0022) ". You're using right quotation marks (U+201D) ”. There's probably a keyboard setting you're using that's causing this, and using certain text editing programs can also prevent this.
Secondly, you seem to be either missing a right bracket. As pointed out, this is probably meant to be part of a list comprehension. You need to write all of the list comprehension for it to work.
The corrected code is:
boombang xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs]

Pattern match(es) are non-exhaustive even though I have specified the pattern [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am writing a function in haskell that checks if something is an element of a list or not.
I have the following code:
elementOf :: Eq a => a ->[a] -> Bool
elementOf _ [] = False
elememtOf x (y:ys) = (x==y) || elementOf x ys
but I am getting the following error:
Pattern match(es) are non-exhaustive
In an equation for ‘elememtOf’: Patterns not matched: _ []
I do not understand why I am getting such error because I have a case for _ [].
Can anyone explain where I am going wrong please?
Thank You!
You misspelled the function name in the last pattern as elememtOf instead of elementOf.

what does forward slash mean in haskell? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am learning Haskell, studying for a midterm. there is a test quiz I'm looking at at the moment with questions on type inference. some of the questions contain forward slashes and I have no idea what they represent. the goal is to determine the type of the function by looking at its variables. here are some examples of the questions im supposed to find the type of:
f1 x = (/x)
f3 x = (x/)
f5 = (1/)
f6 = (/2)
I have googled with no luck, what does the forward slash mean?
These are sections: the expression (/x) is equivalent to \y -> y / x and (x/) is equivalent to \y -> x / y.

Resources