Noobie question - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Mikolaj Kubera

Hi, folks. Noobie Haskell question. How can I create this structure myself: Post {id = c2eacdc4-1828-40bd-921e-d7a939edc9c3, title = "Hello", body = "World", meta = MetaBag {annotations = [], touchedFields = []}}
I come from Elm, so I'm used to type Post = Post { ...record here... }. Thanks :)

Georgi Lyubenov // googleson78
data Post = Post { ...record here... }
Georgi Lyubenov // googleson78

I guess you also need one for MetaBag

Mikolaj Kubera

Motherfuck. Thanks :D Is { key = value } structure also called a record in HS?

Georgi Lyubenov // googleson78

I mean, I would use "record" to refer to the Post type from above, but if I were to be pedantic I would say { key = value } is a syntax error

Georgi Lyubenov // googleson78

you always need the Post constructor here

Georgi Lyubenov // googleson78

then it's "just" creating a Post value

Georgi Lyubenov // googleson78

let x = Post { id = "pesho" ...} in ...

Torsten Schmits

maybe give a full example?

Mikolaj Kubera

@Torsten Schmits I'm just playing around with IHP and was curious how to dig a bit into basic data types.

Torsten Schmits

@Mikolaj Kubera I meant Georgi :sweat_smile:

Mikolaj Kubera

Oh, haha :sweat_smile:

Torsten Schmits

so

data Post = Post {
  id :: UUID,
  meta :: MetaBag
}

post = Post UUID.nil (MetaBag x y z)
post = Post { id = UUID.nil, meta = MetaBag x y z}

like this!

Mikolaj Kubera

Is this "the way" to structure data in Haskell?

Mikolaj Kubera

Awesome. So there's nothing like a record or map or anything key/value specifically that can be used without a constructor?

Torsten Schmits

no, even Bool is not a builtin type

Torsten Schmits

I guess technically [(k, v)] is builtin (i.e. a list of tuples)

Mikolaj Kubera

uhum. OK. Thanks :) Much appreciated

Kampouse

I mean you can build your own

Mikolaj Kubera

@Kampouse Expand the thought, pls :)

Mason Mackaman

@Mikolaj Kubera If you're just messing around with haskell to learn and you're coming from elm, I recommend taking a look a PureScript. I has a lot of the advanced FP stuff haskell has, but it also has records that work similar to elm's

Mikolaj Kubera

@Mason Mackaman Hi. Thanks. Do you then suggetst to use PureScript as a kind of "intermediary" language for me? :) (Although, funny thing, learning a bit of Haskell has already made it easier for me to work with Elm :x)

Mason Mackaman

Honestly, I can't really suggest that because personally I moved from elm to purescript, and I have not done any substantial work in haskell myself. If haskell is your end goal I would say just stick with haskell :+1:

Mason Mackaman

but if your end goal is just to learn about and use advanced FP concepts, then either purescript or haskell will have tons to teach you, and you might find purescript a little nicer to work with.

TheMatten

Haskell has stronger documentation / community content, so that will probably make it easier to pick up
On the other hand, PureScript has more modern (that is, more sensible) standard libraries, because it's younger

Georgi Lyubenov // googleson78

if you're goal is to learn Haskell, you should go to Haskell imo

I think they're close enough in "base" complexity (no extensions) that learning ps won't help too much

Georgi Lyubenov // googleson78

PureScript is definitely closer to Elm than Haskell is, thanks to its anonymous records

Kampouse

I have learn a bit of clojure prior haskell made much more sense

Mason Mackaman

@TheMatten what's the best haskell documentation? do they have something like jordan's reference that is nearly exhaustive? I would love that.

TheMatten

LYAH is fine to get some idea about what language looks like, then Haskell wikibook, Typeclassopedia on Haskell Wiki and WIWIKWLH should cover most stuff you'll encounter and GHC Manual has great documentation when it comes to extensions and flags

Georgi Lyubenov // googleson78

lyah has no exercises though, I think Hutton's book is better in this regard

Mason Mackaman

@Georgi Lyubenov // googleson78 you really think PS's records make it closer to elm than haskell? I'm under the impression that PS and haskell are much closer to each other due to all the advanced concepts they share. I know haskell + extensions has more, but does it really have so many more features that PS isn't even past the halfway point between haskell and elm?

Georgi Lyubenov // googleson78

I meant that PS is closer to Elm, than Haskell is to Elm

Georgi Lyubenov // googleson78
Elm |-------------------------------| PureScript
Elm |-----------------------------------| Haskell
Mason Mackaman

oh, I see you edited the message. I didn't catch that difference on the reread :+1:

Gabriel Lebec

We should mention that Haskell does have a map data structure with dynamic key-value pairs, Data.Map. But it's used like an object in JS, not like a class.

Gabriel Lebec

you can find maps and sets in the containers package.

Gabriel Lebec

But again, not what you are looking for – @Torsten Schmits already showed the idiomatic way to structure data with a "known" structure in Haskell.

Vincent L

What is a "MonadPlus" ?

Torsten Schmits

it's Monad mixed with Alternative. Just a combination of their operations

Torsten Schmits

it's Monad mixed with Alternative. Just a combination of their operations

Vincent L

Does Alternative model something in category theory ?

Torsten Schmits

some kind of functor monoid

Torsten Schmits

don't know, I think just associativity

Pedro Minicz

Yes, (empty <|>) = id = (<|> empty).

Pedro Minicz

Vincent L said:

Does Alternative model something in category theory ?

If it didn't model anything originally, someone has probably developed a categorical model of it, even if just for fun. I couldn't find any such categorical explanation, though. I believe it has something to do with 2-categories or monoidal categories, since the diagrammatic explanations I can give of monads and <|> and empty live in different categories (the monad explanation is a diagram on the category of endofunctors of Hask while the explanation for <|> and empty is a diagram on Hask).

Pedro Minicz

Also, please start new threads and avoid using generic names such as "Noobie questions" as that will later make discussions much more searchable @Vincent L

Vincent L

Is there an equivalent of (!!) with relude ? I'm trying to do import Relude((!!)) but it doesn't work

Torsten Schmits

Relude doesn't export any unsafe combinators

Vincent L

but there is no definition of type (!!) :: [a] -> Int -> Maybe a ?

Peter J. Jones

Keep in mind that [a] is a linked list, not an array. Indexing into a linked list means you have to walk all the links up to the element you are interested in.