31

Computer science students and data scientists rejoice, "All algorithms" implemented in many common languages:

https://github.com/TheAlgorithms

Comments
  • 5
    Ofc you're the one who posts this 😂
  • 1
    I opened a scala code and it was 3 nested for loops. Yeah sorry, im not gonna read past that.
  • 4
    @ganjaman Optimized code is harder to understand. While I guess these aren't to copy and paste into prod, they're definitely great resources for learning how these algorithms work.
  • 1
    @AlgoRythm no, i mean rule #1 of functional programming is to use recursion over loops. This repo is not "x in y language", its "x in C, slightly modified to run in y language"
  • 1
    @ganjaman I see, I was unaware. Functional programming is not my cup of tea. I just love my objects.

    How do functional programs avoid stack overflow when using recursive functions over loops?
  • 1
    @AlgoRythm theres a lot of methods and its one of the harder parts, like choosing head/tail recursion, or using inner maps or seqs, but most overflow issues can be solved with lazy evaluation (basically the next 1 input is considered at any given point, makes infinite recursion semi-possible)
  • 2
    @ganjaman That sounds like yet another reason I'll probably only learn functional programming when I'm old and bored and run out of crosswords.
  • 1
    @AlgoRythm @AlgoRythm lazy evaluation is also one of the reasons why you can write e.g. Fibinacci function in one line and it is so much fucking faster than all conventional methods
  • 0
    User name checks out
  • 1
    @Lythenas Is it really faster than the iterative form with a loop?
  • 1
    @Fast-Nop if you implement them both the same way I expect they have almost the same performance. But if you implement fibs in one line in haskell it probably takes more time to print the numbers then calculate them. I can calculate the 10000 fib pretty much instantly. To get the same performance you would have to implement it either iteratively or cache all fibs in other languages. Also in other languages you need something like BigInt which is usually annoying to work with.
  • 1
    @irene 1. CS students in their homework, and 2. people who want to check whether tail call optimisation works as expected.
  • 0
    @irene Fibonacci is just a very simple example everyone should understand. And it demonstrates nicely why lazyness is a nice feature
  • 0
    @irene we were talking about lazy evaluation a few comments above. And it is one of the main reasons e.g. Haskell is so fast and efficient. If you calculate the 10000th fib you take up literally no ram because you throw away the others. But you can still use the same function to calculate a range of fibs if you want.
  • 0
    @irene if you write a function to return the first n fibs and you just want the nth fib and not the others the calculation will take up the space of n ints. With lazynesa it doesn't because the compiler knows you only want the nth.
    If the function is lazy it is more versatile
  • 0
    @irene my point is you can use the same function to calculate the nth fib and to calculate the first n fibs. And if it's lazy it only uses as much space as it needs to store what you actually use later
Add Comment