Share your repls and programming experiences

← Back to all posts

I think I'm pretty much doing pi approximations in every language now, so, here it is in Haskell!

I was going to do NASM before Haskell (and actually before Raku even), but ran into some issues with floating point numbers that I'm currently working on fixing (if you know how to fix it, please tell me here: https://repl.it/talk/ask/NASM-Print-Floating-Point/41105).

Credit to @NoelBryan, @TheForArkLD , @Warhawk947 , and @LizFoster for inspiring these approximations. Most of these use the Nilakantha Series, although I also use the arctangent method (multiplying the radian arctangent of 1 by 4 to get pi and getting the arctangent with an infinite series) in three of them (Python, Node, and C).

Also, credit to @StudentFires for inspiring this approximation in Haskell!

Next up is NASM (Assembly; only if I can actually figure it out. I might have to save it for later since it is so difficult to program in. I keep pushing it back anyways due to issues with printing floating point numbers), followed by R, followed by Dart. If you have any suggestions for after those, please let me know!

Note: This is slow since Haskell doesn't have loops and only has recursion and it needs to run the same function a lot of times.

HahaYes (1211)

huh, quite intresting

AmazingMech2418 (894)

@HahaYes Thank you! It is quite slow because it requires recursion though...

Zekka (40)

It's probably slow because of how you defined s and a -- each of them loops a large number of times every iteration!

Here's a faster implementation of the same algorithm! https://repl.it/@Zekka/A-faster-Haskell-pi#main.hs

Jakman (450)

Some call it Haskell. I call it magic.

xxpertHacker (387)

@Jakman You just don't know Haskell.

Jakman (450)

@StudentFires indeed. It is the language of the wizards and genuises.

Jakman (450)

@StudentFires I have had people call me hardcore for knowing Rust.

xxpertHacker (387)

@Jakman :) Not really, go look at a tutorial, it's easy.

xxpertHacker (387)

@Jakman Ha, Rust is similarly easy.

Jakman (450)

@StudentFires rust is not hardcore people usually say that because they have only known of python and the blessing of classes and automated garbage collection.

Jakman (450)

@StudentFires in a form. isnt GC done for you in haskell?

xxpertHacker (387)

@Jakman Yes, as it is in any GC language.

Jakman (450)

@StudentFires so it is manual. So you are telling me that haskell is functional Rust?

xxpertHacker (387)

@Jakman No, I updated what I said, I was thinking of something else at the moment.

Jakman (450)

@StudentFires wait so GC is done for you in haskell?

xxpertHacker (387)

Um, I think your doing something wrong, Haskell doesn't have loops, but recursion is optimized to a loop in Haskell. Oh, that's for simple tail recursion.

AmazingMech2418 (894)

@StudentFires I'm not using a loop. I'm using recursion.

xxpertHacker (387)

@AmazingMech2418 Did you read what I wrote at all?

AmazingMech2418 (894)

@StudentFires Yes, but you said you thought I was doing something wrong since Haskell doesn't have loops.

xxpertHacker (387)

@AmazingMech2418 No, I said it gets converted to a loop with optimization, so it's fast anyways.