How to assign to mvar with case syntax? [closed]
Question
I'am asking if i can add same think like that :
if maVar == "false" then maVar = "" else maVar = maVar
in tmy code haskell (yesod)
let maVar= unpack $ case Map.lookup "maVarSession" sess of
Just a -> a
Nothing -> "Nothing"
Solution
No, maVar
is imutable and it is String
always.
In Haskell there aren't any false
. It has False
.
You could do next:
let maVar = ...
in
let maVarNew = if maVar == "Nothing" then "" else maVar
in
OTHER TIPS
It appears you're attempting to assign a new value to maVar
. This does not make sense in Haskell, because Haskell doesn't really have mutable variables,* and you're getting a syntax error because the parser sees =
in a position which does not accept declarations.
Instead, you can declare a new value (we can name it whatever, but let's call it maVar'
), defined maVar' = if maVar == "false" then "" else maVar
.
Then, we can write
let maVar = unpack $ case Map.lookup "maVarSession" sess of
Just a -> a
Nothing -> "Nothing"
maVar' = if maVar == "false" then "" else maVar
[...]
Guards can be also used to write the new declaration (perhaps more clearly, possibly not) as
maVar'
| maVar == "false" = ""
| otherwise = maVar
It would probably be a good idea to make sure you understand what exactly you're doing, however; perhaps read some introductory texts on Haskell to understand purity and how Haskell works differently from languages you might be familiar with.
* You can have a mutable cell, but you really have to ask specifically for it—you can't easily do it by accident.