- cross-posted to:
- programming@lemmy.ml
- cross-posted to:
- programming@lemmy.ml
Real talk, Lisp is the fucking GOAT. The only time I’ve ever been flabbergasted by a program was while learning Lisp, and seeing just how disgustingly efficient it is with recursive algorithms such as Fibonacci. Like, it was limited only by how much ram was available. It seriously had no problem finding the millionth Fibonacci number. Like, what? Write that in C or Fortran and try finding the 34th Fibonacci number.
For me, the biggest selling point is that it’s both expressive and simple. Most languages tend to be one or the other. You have languages like C that are relatively simple to learn, but it takes a lot of code to express what you’re trying to do semantically, and you end up with a lot of incidental boilerplate in the process. At the other end of the spectrum you have languages like Haskell or Scala that let you write very concise code, but the cost is the mental overhead of understanding complex syntax and tons of language rules.
Lisps show that you can have a small language with minimal syntax and simple semantics that can be extremely expressive. Our of all the languages I’ve used over the years, I’ve enjoyed working in Lisp the most. At this point you couldn’t pay me enough to use anything else.
Finding Fibonacci numbers in C is easy, even when using recursion. Here is a program that can find the 1,000,000th Fibonacci number. It’s too big to paste the output of the program here but here you can find it on pastebin here.
#include <stdio.h> #include <gmp.h> void fib(int idx, mpz_t *a, mpz_t *b, mpz_t *c) { mpz_add(*c, *a, *b); mpz_set(*a, *b); mpz_set(*b, *c); if (idx < 0) return; fib(idx - 1, a, b, c); } int main() { mpz_t a; mpz_init(a); mpz_t b; mpz_init(b); mpz_t c; mpz_init(c); mpz_set_str(a, "1", 10); mpz_set_str(b, "0", 10); fib(1000000, &a, &b, &c); mpz_out_str(stdout, 10, c); printf("\n"); mpz_clear(a); mpz_clear(b); mpz_clear(c); return 0; }
You need to compile with the -O2 flag in GCC. If your recursive call is at the end of the function, this is called tail recursion, and when using that flag, the GCC compiler is smart enough to not allocate more memory on the stack every time it does a recursive call, so you don’t run out of memory.
Yes, if you use
gmp.h
, you can do it. Lisp does it out of the box. The problem isn’t tail recursion (although that’s a great way of saving memory), it’s that the size of an integer in a normal language is 64 bits at the maximum. If you have a library such as GMP to coerce that into an array underneath, then it can be done.
Presumably because it has the best syntax ever. Seriously, features aside LISP is just pleasant to write in.
couldn’t agree more
I found a YouTube link in your post. Here are links to the same video on alternative frontends that protect your privacy: