this post was submitted on 19 Apr 2024
511 points (98.3% liked)

Programmer Humor

19817 readers
1052 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] krewjew 32 points 8 months ago (2 children)

I’ve been learning Haskell, and now I won’t shut up about Haskell

[–] affiliate 2 points 8 months ago (2 children)

what's the appeal of haskell? (this is a genuine question.) i've been a bit curious about it for a while but haven't really found the motivation to take a closer look at it.

[–] [email protected] 8 points 8 months ago* (last edited 8 months ago) (1 children)

purely functional paradigm (immutable data structures and no shared state, which is great for e.g. concurrency) and advanced type system (for example you could have linear types that can be only used once). Lisps build on the premise that everything is data, leaving little room for bloated data structures or tight coupling with call chains that are hard to maintain or test. In Haskell on the other hand, everything is a computation, hence why writing it feels more like writing mathematical equations than computer programs somehow. It might, along Scala be good for data-driven applications.
Also the purely functional syntax means that on average, functional programming languages will arrive at the same solution in approx. 4 times less LOC than procedural/OO according to some research. Just look at solutions to competetive programming problems.
And even though I'm not a big fan of opinionated frameworks, compare some Phoenix codebase to a Symfony or even a Rails one to see how much cleaner the code is.

But if you're new to FP you should rather pick Scheme, Elixir or Clojure since the paradigm itself can be a little bit hard enough to wrap your head around at first (though Elixir and is a bit imperative, depends on how deep are you ready to dive in), not to mention having to learn about ADTs and category theory.

[–] krewjew 2 points 8 months ago

My favorite feature is how currying is applied literally everywhere. You can take any function that accepts 2 args, pass in a single arg and return a new function that accepts one arg and produces the result. In Haskell, this is handled automatically. Once you wrap your head around using partially applied and fully saturated functions you can really start to see the power behind languages like Haskell

[–] [email protected] 4 points 8 months ago* (last edited 8 months ago) (1 children)

It's been noted that functional code accumulates less bugs, because there's no way to accidentally change something important somewhere else, and Haskell is the standard for functional languages. Also, it might just be me, but the type system also feels perfect when I use it. Like, my math intuition says there's no better way to describe a function; it's showing the logic to me directly.

Where Haskell is weak is when interactivity - either with the real world or with other components - comes up. You can do it, but it really feels like you're writing normal imperative code, and then just squirreling it away in a monad. It's also slower than the mid-level languages. That being said, if I need to quickly generate some data, Haskell is my no-questions go to. Usually I can do it in one or two lines.

[–] victorz 1 points 8 months ago (1 children)

Out of curiosity, what kind of data do you generate with Haskell? And would be willing to show an example of a one or two liner that generates the data? 🙏

[–] [email protected] 2 points 8 months ago* (last edited 8 months ago) (2 children)

Uh, let's look at my GHCi history...

It looks like I was last searching for 12-member sets of permutations of 7 which come close to generating every possible permutation of seven elements, as well as meeting a few other criteria, for an electronics project. It ended up being more like 10 lines plus comments, though, ~~plus a big table generated by GAP, which I formatted into a Haskell list using probably a line of Haskell plus file loading.~~

Unfortunately for providing code, me playing with the finished algorithm has eaten up my whole 100 lines of history. So, here's a two-liner I posted on Lemmy before, that implements a feed-forward neural net. It's not exactly what you asked for, but it gives you an idea.

layer layerInput layerWeights = map relu $ map sum $ map (zipWith (*) layerInput) layerWeights

foldl layer modelInput modelWeights

In practice, you might also need to define relu in another line:

relu x = if x > 0 then x else 0

Edit: No wait, I think that was a different problem related to the same project. There's another module attached that generates all permutations of n items. After breaking it up so it's a bit less write-only:

allPermutations :: Int -> [[Int]]
allPermutations 1 = [[0]]
allPermutations n = concat $ map (addItem $ allPermutations (n-1) ) [0..(n-1)]

addItem :: [[Int]]  -> Int -> [[Int]]
addItem old new = map (\y -> new : map (fitAround new) y) old

fitAround :: Int -> Int -> Int
fitAround n y
	| y >= n	= y+1
	| otherwise	= y
[–] victorz 1 points 8 months ago (1 children)

Cool! Thank you for sharing!

I just recently read the man page for GNU Parallel, and that seems like a pretty nifty tool to generate permutations of data as well. It's command-line so great for scripts, utilizes many cores, quick to do without mind-bending Haskell project setup, etc..., for anyone interested. 🙂👍

[–] [email protected] 1 points 8 months ago (1 children)

I'll look into it, although this probably wasn't directed at me. GHC can compile multi-threaded, and "setup" here was just opening GHCi and starting, and then moving it into files once it got large enough I didn't want to repeat the work, so I'm happy.

[–] victorz 2 points 8 months ago

I still haven't been able to learn project setup with Haskell and know exactly what I'm doing lol. But I'm glad you have a working setup! But yeah, do look into parallel if you want, maybe it can prove useful, or not. It was directed at you, considering your use case of generating permutations. :-)

Take care!

[–] victorz 1 points 8 months ago* (last edited 8 months ago) (1 children)

BTW: I think you need to put the "```" on separate lines.

test

test

Edit: huh, nope, that had no difference in effect for me. Wonder why your code doesn't render for me...

[–] [email protected] 1 points 8 months ago (1 children)

Which frontend are you on? I'm using lemmy-ui (the default webapp) and it renders fine, not sure how to find which version.

[–] victorz 1 points 8 months ago (1 children)

I was using Sync for Lemmy when your comment didn't render properly. No idea why. Especially since my own comments were rendering fine. 🤷‍♂️

[–] [email protected] 2 points 8 months ago (1 children)

I'd guess it has to do with the specific symbols I was using, and there's a bug in rendering of markdown in Sync.

[–] victorz 1 points 8 months ago
[–] victorz 2 points 8 months ago (1 children)

I learned some Haskell. Did some problems on Advent of Code and such. But since then I've heard about OCaml, which seems super interesting. Hopefully the tooling is simpler, but I've not had time to try anything yet.

Does anybody have any experience with it?

[–] [email protected] 1 points 8 months ago (1 children)

Im pretty sure tsoding has some videos with it

[–] victorz 2 points 8 months ago

I'll check it out, thank you very much! I approximate it a lot. 🙂🙏👍