Pattern matching using existing bound variables

168 views
Skip to first unread message

Michał Podwórny

unread,
Dec 3, 2016, 8:40:41 PM12/3/16
to Elm Discuss
Hi,

Consider this:
let
  x
= 1
in
 
case something of
    x
-> (...)
    _
-> (...)

The compiler will complain that "The following pattern is redundant", pointing to the wildcard. I assume that Elm ignores the fact that "x" is already bound, re-binds it in the first case match and that indeed makes the wildcard redundant. I know how I would do this in Elixir: I'd put "^" before "x" to explicitly say not to re-bind the x variable. Is there something like this in Elm?

Duane Johnson

unread,
Dec 3, 2016, 8:58:22 PM12/3/16
to elm-d...@googlegroups.com
I don't know of a way. Use if...then...else?

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joey Eremondi

unread,
Dec 3, 2016, 9:26:18 PM12/3/16
to elm-d...@googlegroups.com
Nope, you can't do this. Any lower-case variables in a pattern match are newly bound.

Part of this is because equality is not always well defined in Elm i.e. we don't want you to be able to do this:

let
  f : Int -> Int
  f x = ...
  someMaybeVal : Maybe (Int -> Int)
  someMaybeVal = ...
in
  case someMaybeVal of
    Just f -> ...
    Nothing -> ...

If you did this, it would try to compare the function in the Maybe to f, which it can't do. Elixr is dynamically typed, so they'll let you do things like that willy nilly, but we don't want such a program to be allowed in Elm.
(There's a whole bunch of other issues with function equality in Elm, which we won't get into here).

This can make code more verbose in Elm, but if you want to compare values (as opposed to patterns) use comparison functions like ==, and if then else expressions.

Michał Podwórny

unread,
Dec 4, 2016, 6:05:10 AM12/4/16
to Elm Discuss
Thanks for clearing things out!
Message has been deleted

Duane Johnson

unread,
Dec 5, 2016, 3:25:07 PM12/5/16
to elm-d...@googlegroups.com

On Sun, Dec 4, 2016 at 6:53 PM, David Andrews <rand...@gmail.com> wrote:
I expect that you are actually looking to do something like this:
  let
    x
= 1
 
in
   
case
something of
     
Just x -> ...
     
Nothing -> ...

David, are you sure you didn't mean the following?

case something of
   Just 1 -> ... 
   Nothing -> ...

It seems strange to include a shadowed variable `x` as in your expected code.

David Andrews

unread,
Dec 5, 2016, 4:32:12 PM12/5/16
to Elm Discuss
That is what I meant.  I will often use this pattern as follows:

orSomething : Maybe String -> String
orSomething str
=
 
case str of
   
Just str -> str
   
Nothing -> "something"

I realize, though, that I misunderstood the original question.  

Duane Johnson

unread,
Dec 5, 2016, 8:04:07 PM12/5/16
to elm-d...@googlegroups.com
Ah, that makes sense. Take the value out of the Maybe box.

--

Max Goldstein

unread,
Dec 5, 2016, 8:06:20 PM12/5/16
to Elm Discuss
That's exactly Maybe.withDefault "something", which is a great example of partial application.
Reply all
Reply to author
Forward
0 new messages