• CanadaPlus@lemmy.sdf.org
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    8 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
    
      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        1
        ·
        8 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
          1
          ·
          8 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. 🤷‍♂️

    • Victor@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      8 months ago

      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. 🙂👍

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        1
        ·
        8 months ago

        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.

        • Victor@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          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!