0

I am pretty new to Haskell and today I was trying to make a calculator using Haskell (like most people make when learning a new language to get the hold of if statements) and I had trouble using if values with strings. I want to check if the string the user wrote is "plus" (without "") but if I don't the quotation marks (so it'd look like if op == plus) it doesn't recognize it as a string and outputs an error but if on the other hand I use the quotation marks (so it'd look like this if op == "plus") then it looks for the string "plus" with the quotation marks, how can I compare a string to a value without quotation marks?

Case 1:

calculate x op y = do
            if op == "plus"
                        then x+y
                        else x

Result: the program looks for "plus" when calling the function and thus if the input when calling the function is for example "1 plus 3" it will give out an error of ':67:13: error: Variable not in scope: plus :: [Char]

Case 2:

calculate x op y = do
            if op == plus
                        then x+y
                        else x

Result: When trying to load the program I get the error "test.hs:2:33: error: Variable not in scope: plus Failed, modules loaded: none.", so I can't try and call the function obviously.

duplode
  • 33,731
  • 7
  • 79
  • 150
OreOS
  • 43
  • 1
  • 6
  • 2
    Please share the [mcve]. – bereal Apr 14 '17 at 18:22
  • In addition to @bereal's note, Welcome to Stack Overflow! I see you've taken the [tour] already, so I'll just link you to the [help], and also to the article on [ask]. – Delioth Apr 14 '17 at 18:24
  • @bereal Added it into the post Delioth Thank you very much for your help and kind attitude, I'll make sure to read it – OreOS Apr 14 '17 at 18:34
  • Tangential note after looking at the source of your question: don't use tabs in Haskell code. See [*Why shouldn't I mix tabs and spaces?*](http://stackoverflow.com/q/35855170/2751851) for discussion of the matter. – duplode Apr 14 '17 at 18:47
  • @duplode Oh god. `↦` doesn't fit into the monospaced font on FF@Win. _Why has no one edited my answer?_ – Zeta Apr 14 '17 at 18:57

1 Answers1

4

The quotation marks are just part of the syntax of string literals, not their contents. That is, if you write op == "plus", this will be true if (and only if) plus contains the characters 'p', 'l', 'u' and 's' (in that order, obviously) - it does not require (or allow) op to contain any quotes.

So if op == "plus" does not produce True for you even when you think it should, op does not contain what you think it does.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • I think there was a misunderstanding between us, first of all I suggest you look at my newly added examples and perhaps it'll clear things up. Second of all let me try to re-explain myself, what I mean is that my goal is to check if the string 'op' is equal to "plus" (without quotation marks so it'd be `plus`) but when using `op == "plus"` it takes the string as `"plus"` rather than `plus` – OreOS Apr 14 '17 at 18:40
  • 2
    @OreOS 'my goal is to check if the string 'op' is equal to "plus" (without quotation marks so it'd be plus)' That's exactly what `op == "plus"` does. 'when using op == "plus" it takes the string as "plus" rather than plus' That is not true. – sepp2k Apr 14 '17 at 18:45
  • Thank you to everyone who helped and @bereal your example made me realize what I believe was the cause. I think that when running it in ghci as a parameter for a function it was looking for a variable named `plus` rather than taking it as a value and when I input `"plus"` that's when it recognized it as a value rather than a variable name, sorry if this was a bother and thank you again for your help. – OreOS Apr 14 '17 at 19:01