r/purescript • • Jul 17 '25

Is it worth learning PureScript? So far the experience was less than ideal.

I am not a Java or SQL programmer, but I was able to quickly get up and going. The error messages made sense. The documentation had plenty of excellent examples.

Here you get confusing type errors. Some examples on the web appear to be broken on purpose, so they are useless. Type signatures are not examples. There is very little traffic on the discourse. Copilot gives nonsense answers and even contradicts itself.

So, I am asking, is there a light at the end of the tunnel? How did you get over the inability to write a simple variant of a simple project?

Was it worth to persist and why?

9 Upvotes

32 comments sorted by

View all comments

Show parent comment

2

u/GetContented Jul 17 '25

Halogen isn't so basic. I tend to use React & Hooks bindings instead. But maybe you don't know React?

Purescript's aim is to be like Haskell but compile to idiomatic javascript. It doesn't have ports and flags. It compiles to "just javascript". It uses a foreign function interface to "talk to" javascript functions and the DOM.

If you want to emulate Elm then maybe something like elmish is what you'd prefer. There are others, I think. I've not used them tho https://pursuit.purescript.org/packages/purescript-elmish/0.13.0 as far as I can see they don't have Ports tho, so you'd still have to use the Web.DOM library, I think.

Here's some code I asked ChatGPT to generate me that console logs some data out of some element by class. If you look up the APIs docs you can see how they work. I'm sure this code is right because it's very basic, and looks like code I've written recently.

Note how in Purescript we have to deal with being in the Effect monad, which is something you just don't have to do in Elm. It's not as simple as Elm.

module Main where

import Prelude
import Effect (Effect)
import Effect.Console (log)
import Web.DOM (querySelector, toDocument)
import Web.HTML (window)
import Web.HTML.Window (document)
import Data.Maybe (Maybe(..))

main :: Effect Unit
main = do
  win <- window
  doc <- document win
  mEl <- querySelector ".my-class" (toDocument doc)
  case mEl of
    Just el -> log "Found the element!"
    Nothing -> log "No element found."

1

u/Exact_Ordinary_9887 Jul 17 '25

OK, I have my flags

https://github.com/bigos/Pyrulis/blob/c898bc783f701924a609e8e8982411d8f3d11f66/Purescript/parameters-and-configuration/second-attempt/dev/index.html#L8

and the code

https://github.com/bigos/Pyrulis/blob/c898bc783f701924a609e8e8982411d8f3d11f66/Purescript/parameters-and-configuration/second-attempt/src/Main.purs#L18

Does your ChatGpt know why there are 4 errors in your example? How much of the code online is such untested ChatGpt output? I tried Copilot and it was giving me nonsense like: the answer A is not correct, but try answer B or even simply say A.

1

u/GetContented Jul 17 '25

What are the errors? That code looks fine to me.

1

u/Exact_Ordinary_9887 Jul 17 '25
module Main where

import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect (Effect)
import Effect.Console (log)
import Effect.Console (log)
import Prelude
import Web.HTML (window)
import Web.HTML.Window (document)
import Web.DOM.ParentNode as PN
import Web.HTML.HTMLDocument as HD

main :: Effect Unit
main = do
  win <- window
  doc <- document win
  mEl <- PN.querySelector "#script_with_flags" (HD.toDocument doc)
  case mEl of
    Just el -> log "Found the element!"
    Nothing -> log "No element found."

1

u/Exact_Ordinary_9887 Jul 17 '25

this fails the type check, as you see i tired to fiddle with imports, but at my noob level I do not know if it is correct

1

u/Exact_Ordinary_9887 Jul 17 '25

Error found:

in module Main

at src/Main.purs:19:27 - 19:47 (line 19, column 27 - line 19, column 47)

Could not match type

String

with type

QuerySelector

while checking that type String

is at least as general as type QuerySelector

while checking that expression "#script_with_flags"

has type QuerySelector

in value declaration main

1

u/Exact_Ordinary_9887 Jul 17 '25

Your ChatGpt forgot to remind me to run:

spago install maybe

spago install web-html

spago install web-dom

After this I reduced error to

Error found:
in module Main
at src/Main.purs:10:17 - 10:30 (line 10, column 17 - line 10, column 30)

  Cannot import value querySelector from module Web.DOM
  It either does not exist or the module does not export it.

1

u/GetContented Jul 17 '25

I almost asked if you knew you had to install libraries. :) Glad you figured that out. Thanks for sharing the error!

So check in Web.DOM's API that querySelector is there, then. That's straightfoward isn't it? I'm assuming you know pursuit if you've read the book you mentioned. Or at least the basics of that book, or if you've read the intro to purescript.

To do this, go into pursuit, then search for querySelector, then look for the one that matches the right capitalisation, then notice that it's moved into a different sub module. Change it and save/recompile.

I would be careful expecting everything to be easy if you don't learn the basics. It's absolutely not. It'll burn you and you won't want to continue. Please don't do that! This is one of the most joyous FE programming experiences there is, so long as you upgrade your understanding to a certain level. It can certainly be frustrating at times because there are a few weird edges, but learning the basics pretty much solves most of them.

0

u/Exact_Ordinary_9887 Jul 17 '25

Pursuit which query selector I should use? there are 10 if I am not mistaken.

I need DOM related, but which one?

Can you see how your advice is missing the target?

1

u/GetContented Jul 17 '25

I think perhaps you might have missed some of my suggestion.

As I said... check in Web.DOM's API that querySelector is there.

If you search in pursuit for querySelector, yes you'll find 10, but if you then look at where they are in the results and see ones that are within that module, as I said. You'll find there's only one in Web.DOM. It's been moved to a sub module called Web.DOM.ParentNode by the looks of things. That's still within Web.DOM.

1

u/ruby_object Jul 17 '25

https://github.com/bigos/Pyrulis/blob/3cd62b640b392db711c2bf375b7e84aaa38c9d97/Purescript/parameters-and-configuration/second-attempt/src/Main.purs#L37

We have lift off!

to make matters worse, the answer was here:

https://book.purescript.org/chapter8.html

Why did nobody point me to the right page and fragment?

I spent 4 days on that!

1

u/ruby_object Jul 18 '25

But in the end I had success.

1

u/ruby_object Jul 22 '25

Trying to figure out how to have a working HTTP POST request took me days. Maybe I have proven I can pick up the basics at a very slow pace. However, I would like to know where I can have a better experience. The beginner level examples are lacking.

While I eventually found the cookbook, adapting the example took way too long.

0

u/Exact_Ordinary_9887 Jul 17 '25

1

u/GetContented Jul 17 '25

Why are you looking in Halogen?

1

u/GetContented Jul 17 '25

Yeah that's using the library I'm suggesting you use. You can use that if you like.