Comment

Comment by thewakalix

> For example, working with Maybe is often frustrating in functional languages. In Kind, we have a default operator: [<>]

While (<>) in Haskell already means a monoidal operator, something similar is frequently defined, such as:

    (:?) :: Maybe a -> a -> a
    (:?) = flip Data.Maybe.fromMaybe
(Making an alternative or supplemental Prelude out of functions like this is a common step in the evolution of a Haskell programmer, like writing a monad tutorial.)

> An "abort" operator, that flattens your code:

That particular function can be written even more concisely in Haskell:

    sumFirst3 :: [Natural] -> Natural
    sumFirst3 list
      | (x : y : z : _) <- list = x + y + z
      | otherwise = 0
And in other cases, the Either monad provides the same logic:

    sumFirst3' :: [Natural] -> Natural
    sumFirst3' list = fromEither do
      x <- list !!? 0 `abort` 0
      y <- list !!? 1 `abort` 0
      z <- list !!? 2 `abort` 0
      return $ x + y + z

    abort :: Maybe a -> e -> Either e a
    abort (Just a) _ = Right a
    abort Nothing e = Left e
(fromEither and (!!?) are utility functions like (:?). For that matter, abort is just a shorter name for maybeToRight.)

> And, of course, Maybe monad:

As it happens, you can write Haskell with braces if you really want to! [0]

[0] https://en.wikipedia.org/wiki/Haskell_features#Layout

parent thewakalix 5 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton