DEV Community

Cover image for Haskell as an alternative to TypeScript

Haskell as an alternative to TypeScript

digitallyinduced on August 11, 2021

If you've been using TypeScript for your web development, you have understood the value that static typing brings to your productivity. However, Ty...
Collapse
 
leob profile image
leob • Edited

But can it also be used for the front end (client side, e.g. through Web Assembly? same solution as used by Rust), or are you only talking about replacing JS/TS for the backend (node.js)? That didn't really become clear to me from your article.

I think Haskell has a lot of similarities with Rust, and both can be used in the same scenarios, to replace JS/TS.

Collapse
 
digitallyinduced profile image
digitallyinduced

I personally don't have experience with compiling Haskell to Web Assembly. There seems to be a compiler for this based on ghc here: github.com/tweag/asterius

The easiest for an existing frontend infrastructure is to use Haskell as a replacement for a nodejs backend.

If you want to take a look at IHP, the solution we're doing there is to put as much logic and code on the server, and to use tools such as the included autoRefresh to still achieve highly interactive webapps. You can read more about autoRefresh here: dev.to/digitallyinduced/displaying...

Rust certainly has similarities to Haskell, so if you like Rust, I highly recommend checking out Haskell too, as the differences will at the very least be a good learning opportunity.

Collapse
 
leob profile image
leob • Edited

I've looked at Haskell in the past, and then at Rust more recently, and I couldn't help but notice how many similarities there are between them (at a conceptual level), even though Rust isn't strictly an FP language.

Thread Thread
 
digitallyinduced profile image
digitallyinduced

There certainly are many similarities. Probably the most popular are the Optional/Maybe and Result/Either types that I also mentioned in the article.

It'd be cool if you could declare functions as using IO/not using IO in Rust too - I think that is a great feature, which has often meant that debugging behavior and making sense of someone else's code got much easier. Though Rust's focus on lower-level programming (compared to Haskell at least) might make it less useful. I don't have much experience with that though - I've always focused on higher-level.

Thread Thread
 
leob profile image
leob • Edited

Yes indeed, the way "optional" values are treated - same story for errors/exceptions, both Haskel and Rust have no try/catch, errors are propagated via return values. Rust also seems to utilize "algebraic data types" in the same fashion as Haskell does, whereas other languages would use more of an OO approach with classes/objects. Following along with the Rust tutorial I constantly thought "deja vue" and then "Haskell" !

And both are impressive in how much checking the compiler does for you - if used properly you can achieve a large degree of confidence that your code "does the right thing" even before running it.

The only thing Rust seems to be missing is tail recursion, this also stops it from being seen as a "pure" FP language, typically you'd still write imperative/procedural loops in Rust.

Collapse
 
leob profile image
leob • Edited

Right, AutoRefresh, this reminds me of old TurboLinks of Rails fame, and more recently of some other "auto Ajax" frameworks which provide for easy and automatic dynamic updates without having to go all out on SPA + API. Can't remember the name of those tools right now, but this definitely seems a trend and a recurring theme recently.

Thread Thread
 
digitallyinduced profile image
digitallyinduced

You might have heard of Phoenix' LiveView. That's certainly very similar.

It's a great pattern - if you haven't used it before I highly recommend trying it out. When I first heard about it I was quite skeptical of whether it could compete with a SPA, but it has certainly convinced me after trying it out.

From the user's point of view there's no difference to be felt really, and as a developer it is SO MUCH nicer.

Thread Thread
 
leob profile image
leob

You're right ... LiveWire (of Laravel fame) is another one, this really seems to be a popular trend, and for good reasons.

Collapse
 
luismed15971068 profile image
luis medina

I am new to this program, I am learning Haskell for several months, I understand that Rust was very inspired by Haskell. I like both (especially the Syntax of the Rust Lambdas 🤤). Excuse me My English please

Collapse
 
leob profile image
leob

Yes there are many similarities between Haskell and Rust, and not just superficial ones, it runs pretty deep!

Thread Thread
 
luismed15971068 profile image
luis medina

Do you know any project where I can practice rust on a personal level?, I mean something that I can show in the future

Thread Thread
 
leob profile image
leob • Edited

Sorry no nor really, I was involved in a project which used Rust long ago, but all I did was work through the Rust manual, the project fell by the wayside and I didn't actually build anything ... well I'm sure there's plenty of project ideas around if you google it a bit, using it on the client with Web Assembly seems popular, but Rust is of course also great as a server side language. I don't know, maybe try to build a chat server and client with it?

Thread Thread
 
luismed15971068 profile image
luis medina

Well, the language I chose to learn in depth is haskell, I heard that the best way to learn in any science is by working on it, that is why I am looking for projects with which to practice 1st Haskell 2nd rust.

Thread Thread
 
leob profile image
leob

Well, like I said you could try to build a chat app, backend and frontend, that sounds like a nice challenge and should keep you off the street for some time, haha ... and maybe have a look at this link:

users.rust-lang.org/t/recommended-...

Collapse
 
lucamug profile image
lucamug

Nice post, thank you for writing it!

Collapse
 
digitallyinduced profile image
digitallyinduced

Thank you for appreciating it!

Collapse
 
luismed15971068 profile image
luis medina

Has anyone used Yesod framework ?, I want to start a personal project to practice my haskell skills.

Collapse
 
digitallyinduced profile image
digitallyinduced

Hello Luis, seeing as you want to start learning Haskell (which is great!), I would suggest you start working through IHP's getting started guide (ihp.digitallyinduced.com/Guide/ind...), with which you will build a simple blog website using Haskell. You can then go and build any other website you want with the foundation that you learn there.

You can try building a similar project with Yesod, though we specifically built IHP for people to be more productive faster, and have an easier time getting started than if they were to use Yesod for example.

Good luck on you journey!

Collapse
 
luismed15971068 profile image
luis medina

Thank very much

Thread Thread
 
digitallyinduced profile image
digitallyinduced

No problem!

Collapse
 
the1nternet profile image
Blyler Blones • Edited

Hi, at the end of the first paragraph, you said, "(but APIs work just fine as well)". How do you create api's in IHP? There is no token authentication and the idea of "session" in IHP requires cookies. Also, the "currentUser" in IHP requires cookies. None of that works with an api. So how would you do authentication and getting the current user, etc, if you're using IHP to build a JSON api?