[SOLVED] Pattern-matching with a variable takes incorrect branch

I stumbled upon a problem which I reduced to this minimal testcase. So, basically, for some reason, if I pattern-match a string with a variable, the pattern-match succeeds even though it shouldn’t.

module Main where

import Prelude

import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
  let foo = "foo"
  log $ case "bar" of
    foo -> "incorrect match"
    _ -> "matched correctly"

Here, I match "foo" against "bar". So you’d expect match should fail, right? But it doesn’t! Instead it takes the branch “incorrect match”, i.e. it thinks “foo” is equal to “bar”.

Is this expected behavior, do I miss something?

Found the answer in this Haskell thread (PureScript syntax is ± Haskell): such pattern-match simply doesn’t work, the variable is being rebound to the value “case-of” works on.

That’s pretty sad, because in real code I have 4 variables I need to match at the same time. I was trying to implement that with [var1, var2, var3 var4] -> …, but instead I guess I have to write x == var1 && y == var2 && …. Oh well…

1 Like