News for hackers. Stuff that matters.
Re:How do I modify a variable in Haskell? by MichaelBurge on Tuesday August 15, 2017 @08:47AM (#15017656)
Thanks for reading!

The idea behind this article is that somebody who asks the question is probably confused about a lot of the core concepts in Haskell. So I write the same code in 20 different ways so that they can use their understanding of earlier concepts to understand later ones. Which hopefully makes them less confused.

That's a different goal than selling people on Haskell, or giving them the best solution and moving on. There is definitely a learning curve before you can hack out code without thinking too much about it.

Re:How do I modify a variable in Haskell? by jordigh on Tuesday August 15, 2017 @08:54AM (#15017702)
I wish there was a canonical, common, simple, straightforward and nearly unambiguous and universal way to modify an element in an array.

In many languages: arr[idx] = nevalue

In Haskell: a blog post or two, telling me how great all the new ways are because of the type system.

I know I'm sounding very contrarian, but it really is a frustration I have with the language. I've heard several times that once you manage to use it a lot you find these unambiguous universal methods. I just feel like getting there first requires digesting all of these blog posts.

Re:How do I modify a variable in Haskell? by frubar on Tuesday August 15, 2017 @01:13PM (#15020163)
>In many languages: arr[idx] = nevalue

The thing you have to keep in mind is that this kind of naked mutation is probably the main source of bugs in programming today. One of the main advantages of OO was that it demarcated which functions could modify a "global" variable. But if I get a weird value in one of my fields, I still can't trivially tell how it got there, I can only narrow down to methods of the class (and possibly the inheritance tree, depending on variable visibility).

Due to that, there is more ceremony in modifying things in Haskell. The language works best if you write most of your code in a way that doesn't mutate anything and then limit mutation to a small area using the advanced techniques developed in the language. But doing all this requires a pretty substantial investment so you probably need something to convince you that the end will be worth it before you start. I don't know what you tell you, what got me interested was something like:

> fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

and

> max = head . sort

but I understand that's not going to motivate most people to completely change how they approach programming as a discipline.

Re:How do I modify a variable in Haskell? by mej10 on Tuesday August 15, 2017 @09:02AM (#15017765)
arr // [(idx,newvalue)]

(//) is a function that takes an array and a list of pairs of indexes and values and returns an array with those indexes set to those values.

Re:How do I modify a variable in Haskell? by ashark on Tuesday August 15, 2017 @10:24AM (#15018420)
This is reinforcing my impression of Haskell's idiomatic aesthetic as "really bad Perl".
Re:How do I modify a variable in Haskell? by mej10 on Wednesday August 16, 2017 @08:59AM (#15027075)
That's a shame, since once you understand it it really is beautiful.
Re:How do I modify a variable in Haskell? by jordigh on Tuesday August 15, 2017 @10:05AM (#15018251)

    Prelude> let x = [1, 2, 3]
    Prelude> x
    [1,2,3]
    Prelude> x // [(2,42)]

    <interactive>:5:3:
        Not in scope: `//'
        Perhaps you meant one of these:
          `/' (imported from Prelude), `/=' (imported from Prelude)
So, how canonical is this if it's not in Prelude?
Re:How do I modify a variable in Haskell? by mitchty on Tuesday August 15, 2017 @11:03AM (#15018849)
The prelude isn't everything in Haskell. Just like you need to import sys or do import re in python to get at things less used here too you have to do the same.

    λ :m Data.Array
    λ let x = array (1,3) [(1,1), (2,2), (3,3)]
    x :: (Num e, Num i, Ix i) => Array i e
    λ x
    array (1,3) [(1,1),(2,2),(3,3)]
    it :: (Ix i, Num i, Num e) => Array i e
    λ x // [(2,42)]
    array (1,3) [(1,1),(2,42),(3,3)]
    it :: (Num i, Num e, Ix i) => Array i e
    λ x
    array (1,3) [(1,1),(2,2),(3,3)]
    it :: (Ix i, Num i, Num e) => Array i e
Note however that this isn't mutating x directly, to do mutation would require state or a monad.
Re:How do I modify a variable in Haskell? by tome on Tuesday August 15, 2017 @10:39AM (#15018560)
Further to the other answers given, `[1, 2, 3]` is not an array, but rather a list.
Re:How do I modify a variable in Haskell? by berdario on Tuesday August 15, 2017 @10:23AM (#15018413)
The Prelude is just what's visible in the default namespace, it's not the whole language

Data.Array and // are also described in the Haskell 2010 report

https://www.haskell.org/onlinereport/haskell2010/haskellch14...

Re:How do I modify a variable in Haskell? by mej10 on Tuesday August 15, 2017 @10:16AM (#15018344)
I understand the source of your question -- not all languages provide arrays in the included-by-default namespace. Python for example requires you to import an array module. These are part of the standard library in both cases.

This makes it a little harder to get started but considering the kinds of things programmers put up with on a daily basis is extremely mild.

Re:How do I modify a variable in Haskell? by _polymer_ on Tuesday August 15, 2017 @09:53AM (#15018148)
I assumed there must be something like this, but that notation is a pain.

What's wrong with something like "set arr idx newvalue"?

Re:How do I modify a variable in Haskell? by tome on Tuesday August 15, 2017 @09:56AM (#15018176)
> What's wrong with something like "set arr idx newvalue"?

Nothing. See here: https://news.ycombinator.com/item?id=15017853

It's called "writeArray".