News for hackers. Stuff that matters.
Re:Functional Semantics in Imperative Clothing (Richard Feld... by skulk on Monday April 29, 2024 @08:53AM (#40197665)
one advantage of this over do-notation is brought up in the article (though not explicitly): you can bind multiple things in a single expression. In Haskell, you'd have to do

    do x <- getx; y <- gety; x+y
In roc you can do

    getx! + gety!
which looks a bit cleaner.
Re:Functional Semantics in Imperative Clothing (Richard Feld... by bradrn on Monday April 29, 2024 @08:58AM (#40197718)
This is really just a matter of how much syntax sugar you want to implement. Idris already has precisely this syntax, and there’s a proposal to add it to Haskell too [0]. But none of this changes the core properties of the system which make it monadic.

[0] https://github.com/ghc-proposals/ghc-proposals/issues/527

Re:Functional Semantics in Imperative Clothing (Richard Feld... by rtfeldman on Monday April 29, 2024 @09:01AM (#40197753)
TIL that Idris also has a very similar `!` operator (in the prefix position instead of suffix) - thank you for sharing!
Re:Functional Semantics in Imperative Clothing (Richard Feld... by alipang on Monday April 29, 2024 @04:36PM (#40203816)
Even Javascript has a similar syntax, though `yield` / `await` only works for certain "monads" (promises seem close enough)
Re:Functional Semantics in Imperative Clothing (Richard Feld... by mrkeen on Monday April 29, 2024 @09:31AM (#40198115)

  (+) <$> getx <*> gety
Re:Functional Semantics in Imperative Clothing (Richard Feld... by superlopuh on Monday April 29, 2024 @11:23AM (#40199525)
Definitely doesn't look as clean as the example above, or as clean as the Swift equivalent of

    await x + y
Re:Functional Semantics in Imperative Clothing (Richard Feld... by mrkeen on Tuesday April 30, 2024 @05:37AM (#40208972)
The same syntax works across a range of constructs in Haskell, Scala and Idris.

  (+) <$> getx <*> gety
This could be:

* summing two reads in a Transaction,

* summing two parsed numbers in a Parser,

* summing two Nullable values into one Nullable value,

* producing a Validated<T> by validating two of its parts which require validation,

* and on and on.

It's even the cartesian product when run on lists:

  let suits = ["♠", "♥", "♦", "♣"]
  let nums  = concat [["A"], map show [2..10], ["J","Q","K"]]
  let cards = (++) <$> nums <*> suits

  putStrLn (unwords (take 15 cards))
  A♠ A♥ A♦ A♣ 2♠ 2♥ 2♦ 2♣ 3♠ 3♥ 3♦ 3♣ 4♠ 4♥ 4♦
Swift, Roc, Rust and JS have specialised their syntax for just one of these concerns.

Also, it looks like you can just do it with ! in Haskell: https://hackage.haskell.org/package/monadic-bang

Re:Functional Semantics in Imperative Clothing (Richard Feld... by tome on Tuesday April 30, 2024 @08:04AM (#40210088)
> it looks like you can just do it with ! in Haskell

Yes, although it's slightly clumsy.

    example = do
      let suits = ["♠", "♥", "♦", "♣"]
      let nums  = concat [["A"], map show [2..10], ["J","Q","K"]]
      let cards = do pure (!nums ++ !suits)
      putStrLn (unwords (take 15 cards))

    ghci> example 
    A♠ A♥ A♦ A♣ 2♠ 2♥ 2♦ 2♣ 3♠ 3♥ 3♦ 3♣ 4♠ 4♥ 4♦
Re:Functional Semantics in Imperative Clothing (Richard Feld... by jimbokun on Monday April 29, 2024 @11:02AM (#40199231)
The Roc version is still cleaner.