• MareOfNights@discuss.tchncs.de
    link
    fedilink
    arrow-up
    0
    ·
    3 months ago

    I never looked into this, so I have some questions.

    Isn’t the overhead of a new function every time going to slow it down? Like I know that LLVM has special instructions for Haskell-functions to reduce overhead, but there is still more overhead than with a branch, right? And if you don’t use Haskell, the overhead is pretty extensive, pushing all registers on the stack, calling new function, push buffer-overflow protection and eventual return and pop everything again. Plus all the other stuff (kinda language dependent).

    I don’t understand what advantage is here, except for stuff where recursive makes sense due to being more dynamic.

    • TechNom (nobody)@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 months ago

      They aren’t talking about using recursion instead of loops. They are talking about the map method for iterators. For each element yielded by the iterator, map applies a specified function/closure and collects the results in a new iterator (usually a list). This is a functional programming pattern that’s common in many languages including Python and Rust.

      This pattern has no risk of stack overflow since each invocation of the function is completed before the next invocation. The construct does expand to some sort of loop during execution. The only possible overhead is a single function call within the loop (whereas you could have written it as the loop body). However, that won’t be a problem if the compiler can inline the function.

      The fact that this is functional programming creates additional avenues to optimize the program. For example, a chain of maps (or other iterator adaptors) can be intelligently combined into a single loop. In practice, this pattern is as fast as hand written loops.

    • TechNom (nobody)@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      ·
      3 months ago

      I looked at the post again and they do talk about recursion for looping (my other reply talks about map over an iterator). Languages that use recursion for looping (like scheme) use an optimization trick called ‘Tail Call Optimization’ (TCO). The idea is that if the last operation in a function is a recursive call (call to itself), you can skip all the complexities of a regular function call - like pushing variables to the stack and creating a new stack frame. This way, recursion becomes as performant as iteration and avoids problems like stack overflow.

      • aubeynarf@lemmynsfw.com
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        Not just calls to self - any time a function’s last operation is to call another function and return its result (a tail call), tail call elimination can convert it to a goto/jump.

    • affiliate@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      3 months ago

      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.

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        3 months ago

        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.

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          3 months ago

          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? 🙏

          • CanadaPlus@lemmy.sdf.org
            link
            fedilink
            arrow-up
            0
            ·
            edit-2
            3 months ago

            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
            
            • Victor@lemmy.world
              link
              fedilink
              arrow-up
              0
              ·
              edit-2
              3 months ago

              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…

              • CanadaPlus@lemmy.sdf.org
                link
                fedilink
                arrow-up
                0
                ·
                3 months ago

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

                • Victor@lemmy.world
                  link
                  fedilink
                  arrow-up
                  0
                  ·
                  3 months ago

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