Radical | 21 Dec 18:46 2012
Picon

Substituting values

Sometimes I'll need something like:

  if value == Foo then Bar else value

Or some syntactic variation thereof:

  case value of { Foo -> Bar; _ -> value }

Is there a better/shorter way to do it? I'm surprised that it's more complicated to substitute a value on its own than e.g. in a list, using filter. Or perhaps I'm missing the right abstraction?

Thanks,

Alvaro



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Kim-Ee Yeoh | 22 Dec 06:35 2012

Re: Substituting values

This is reminiscent of the Either (exception) monad where Left values, the exceptions, pass through unaltered, and Right values are transformable, i.e. acted on by functions.


But I have no idea what you're trying to achieve in the bigger picture. Help us help you by fleshing out your use case.

-- Kim-Ee


On Sat, Dec 22, 2012 at 12:46 AM, Radical <radical <at> google.com> wrote:
Sometimes I'll need something like:

  if value == Foo then Bar else value

Or some syntactic variation thereof:

  case value of { Foo -> Bar; _ -> value }

Is there a better/shorter way to do it? I'm surprised that it's more complicated to substitute a value on its own than e.g. in a list, using filter. Or perhaps I'm missing the right abstraction?

Thanks,

Alvaro




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Radical | 25 Dec 02:43 2012
Picon

Re: Substituting values




On Sat, Dec 22, 2012 at 12:35 AM, Kim-Ee Yeoh <ky3 <at> atamo.com> wrote:
This is reminiscent of the Either (exception) monad where Left values, the exceptions, pass through unaltered, and Right values are transformable, i.e. acted on by functions.


Interesting. I hadn't thought of that parallel. But yes, I think it's a similar notion of: transform the value only in one (or more) cases. It's also similar to the Maybe monad in that I'm looking for something akin to fromMaybe, but without having to place the value in a Maybe.
 
But I have no idea what you're trying to achieve in the bigger picture. Help us help you by fleshing out your use case.


I'm not sure the bigger picture is helpful, but, for example, the last case was something like: 

  if ch == '\n' then ' ' else ch

I think I ended up rewriting it as something like:

  replace '\n' = ' '
  replace ch = ch

Which is straightforward enough, though it feels to me like there's too much syntax involved.

Thanks,

Alvaro



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
David Thomas | 22 Dec 12:46 2012
Picon

Re: Substituting values

Seems like the function is easy to define:

replaceIfEq a b c = if c == a then b else c

Then the above can be written

   replaceIfEq Foo Bar value

Or the slightly more general (in exchange for slightly more verbosity at the call site)

replaceIf p r a = if p a then r else a

   replaceIf (== Foo) Bar value




On Fri, Dec 21, 2012 at 6:46 PM, Radical <radical <at> google.com> wrote:
Sometimes I'll need something like:

  if value == Foo then Bar else value

Or some syntactic variation thereof:

  case value of { Foo -> Bar; _ -> value }

Is there a better/shorter way to do it? I'm surprised that it's more complicated to substitute a value on its own than e.g. in a list, using filter. Or perhaps I'm missing the right abstraction?

Thanks,

Alvaro




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Radical | 25 Dec 02:52 2012
Picon

Re: Substituting values

Yeah, I guess I was wondering whether something like it already existed. Thinking about it some more, perhaps what I want is `tr` [1]. E.g.

  tr [Foo] [Bar]

Incorporating your suggestion would then yield a more general version like:

  trBy (== Foo) Bar





On Sat, Dec 22, 2012 at 6:46 AM, David Thomas <davidleothomas <at> gmail.com> wrote:
Seems like the function is easy to define:

replaceIfEq a b c = if c == a then b else c

Then the above can be written

   replaceIfEq Foo Bar value

Or the slightly more general (in exchange for slightly more verbosity at the call site)

replaceIf p r a = if p a then r else a

   replaceIf (== Foo) Bar value




On Fri, Dec 21, 2012 at 6:46 PM, Radical <radical <at> google.com> wrote:
Sometimes I'll need something like:

  if value == Foo then Bar else value

Or some syntactic variation thereof:

  case value of { Foo -> Bar; _ -> value }

Is there a better/shorter way to do it? I'm surprised that it's more complicated to substitute a value on its own than e.g. in a list, using filter. Or perhaps I'm missing the right abstraction?

Thanks,

Alvaro




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Petr P | 22 Dec 15:52 2012
Picon

Re: Substituting values

Hi Alvaro,

I think you need something wha Scala has - the ability to create a partial function from a case expression. In Scala you could write

  def update[A](f: PartialFunction[A,A])(v: A): A =
    f.orElse({ case x => x } : PartialFunction[A,A]).apply(v);

and then use it like

  update[Int]({ case Foo => Bar })

But AFAIK there is nothing like this in Haskell. Maybe separating 'of' from 'case' would be the way to extend Haskell with such a feature <http://www.haskell.org/pipermail/haskell-cafe/2012-November/104884.html>

Best regards,
Petr



2012/12/21 Radical <radical <at> google.com>
Sometimes I'll need something like:

  if value == Foo then Bar else value

Or some syntactic variation thereof:

  case value of { Foo -> Bar; _ -> value }

Is there a better/shorter way to do it? I'm surprised that it's more complicated to substitute a value on its own than e.g. in a list, using filter. Or perhaps I'm missing the right abstraction?

Thanks,

Alvaro




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Radical | 25 Dec 21:01 2012
Picon

Re: Substituting values

Hey Petr,

On Sat, Dec 22, 2012 at 9:52 AM, Petr P <petr.mvd <at> gmail.com> wrote:
I think you need something wha Scala has - the ability to create a partial function from a case expression. In Scala you could write

  def update[A](f: PartialFunction[A,A])(v: A): A =
    f.orElse({ case x => x } : PartialFunction[A,A]).apply(v);

and then use it like

  update[Int]({ case Foo => Bar })


Thanks for the pointer. One distinction I remember from dabbling in Scala some years ago is that Scala seems to formalize partiality whereas, if I'm not mistaken, Haskell doesn't (though perhaps there are libraries that let you do that). That is, to me a partial function in Haskell is almost uniformly an error, whereas in Scala it's a somewhat common pattern.
 
But AFAIK there is nothing like this in Haskell. Maybe separating 'of' from 'case' would be the way to extend Haskell with such a feature <http://www.haskell.org/pipermail/haskell-cafe/2012-November/104884.html>

Does Haskell have a way to query at runtime whether a function is partial? (Not in the full sense, since it would be necessary to prove totality, but in the sense that a case expression is not exhaustive.) Otherwise, I'm not sure how you could use a partial lambda without reaching `undefined`. (I guess you could catch the exception.)

Thanks,

Alvaro





_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Jason Dusek | 22 Dec 16:12 2012
Picon

Re: Substituting values

2012/12/21 Radical <radical <at> google.com>:
> Sometimes I'll need something like:
>
>   if value == Foo then Bar else value
>
> Or some syntactic variation thereof:
>
>   case value of { Foo -> Bar; _ -> value }
>
> Is there a better/shorter way to do it? I'm surprised that it's more
> complicated to substitute a value on its own than e.g. in a list, using
> filter. Or perhaps I'm missing the right abstraction?

Haskell doesn't offer a compact "ternary operator" or similar
construct. In some cases, a local definition and pattern guards
is appealing:

  {-# LANGUAGE PatternGuards #-}

  f value = ... value' ...
   where value' | Foo <- value = Bar
                | otherwise    = value

This does not really have the intuitive of appeal that a
"pattern matching ternary operator" might, though:

  f value = ... (Foo <- value ? Bar : value) ...

In Haskell, working with patterns generally is cleaner with
multiple lines and full indentation.

--
Jason Dusek
pgp // solidsnack // C1EBC57DC55144F35460C8DF1FD4C6C1FED18A2B
Chaddaï Fouché | 26 Dec 19:14 2012
Picon

Re: Substituting values

Since recently, the notion of prisms from the lens library can achieve
that : to modify a value only in certain conditions but you have to
write the prism so it's not that convenient, though at least you'll
have an uniform API.
See http://hackage.haskell.org/packages/archive/lens/3.7.0.2/doc/html/Control-Lens-Prism.html
, especially the "nat" example.

--
Jedaï

On Fri, Dec 21, 2012 at 6:46 PM, Radical <radical <at> google.com> wrote:
> Sometimes I'll need something like:
>
>   if value == Foo then Bar else value
>
> Or some syntactic variation thereof:
>
>   case value of { Foo -> Bar; _ -> value }
>
> Is there a better/shorter way to do it? I'm surprised that it's more
> complicated to substitute a value on its own than e.g. in a list, using
> filter. Or perhaps I'm missing the right abstraction?
>
> Thanks,
>
> Alvaro
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane