Programming thread

  • 🔧 Chat is under maintenance.
    🧊 New server is in an office waiting on a mexican.
  • Want to keep track of this thread?
    Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
    Create account
Still interested in Lisp but I managed to make some little functions in Haskell. Haskell has always fascinated me for being a difficult language. Only the really smart people write Haskell. I had fun with it.
Code:
-- tail factorial function in haskell

tfac :: Integer -> Integer -> Integer

tfac x y = if x < 2 then y else tfac (x - 1) (x * y)

collatz :: Integer -> Integer -> Integer
collatz c d
    | c == 1 = d
    | odd c = collatz ((3 * c) + 1) (d + 1)
    | not (odd c) = collatz (c `div` 2) (d + 1)


main :: IO()
--main = tfac 6 1
main = do
    let a = tfac 6 1
    -- let answer = show a
    --putStrLn answer
    putStrLn (show a)
    let b = collatz 13 1
    putStrLn (show b)
 
Still interested in Lisp but I managed to make some little functions in Haskell. Haskell has always fascinated me for being a difficult language. Only the really smart people write Haskell. I had fun with it.
Code:
-- tail factorial function in haskell

tfac :: Integer -> Integer -> Integer

tfac x y = if x < 2 then y else tfac (x - 1) (x * y)

collatz :: Integer -> Integer -> Integer
collatz c d
    | c == 1 = d
    | odd c = collatz ((3 * c) + 1) (d + 1)
    | not (odd c) = collatz (c `div` 2) (d + 1)


main :: IO()
--main = tfac 6 1
main = do
    let a = tfac 6 1
    -- let answer = show a
    --putStrLn answer
    putStrLn (show a)
    let b = collatz 13 1
    putStrLn (show b)
It's a good pattern if you want to use accumulator to put it in a where clause with helper function.
Code:
 fac :: Integer -> Integer
 fac n = go n 1
   where
     go 0 acc = acc
     go n acc = go (n-1) (acc*n)
 
When I tried haskell 10 years ago I found that pattern matching was awesome, but then I found myself needing to divide a float by an integer and I tried for 15 minutes and couldn't do it so I gave up forever.
Haskell doesn't allow for automatic type conversions so since "/" has following signature:
Code:
class Num a => Fractional a where
  (/) :: a -> a -> a
both parameters have to be of the same type, that implements Num and Fractional typeclass.

It can be easily done by using fromIntegral
Code:
fromIntegral :: (Integral a, Num b) => a -> b
which can convert Integral type into any Num type.

Code:
ghci> let x = 10 :: Integer
ghci> :i x
x :: Integer    -- Defined at <interactive>:9:5
ghci> 22.4 / x
<interactive>:11:1: error: [GHC-39999]
    • No instance for ‘Fractional Integer’
        arising from the literal ‘22.4’
    • In the first argument of ‘(/)’, namely ‘22.4’
      In the expression: 22.4 / x
      In an equation for ‘it’: it = 22.4 / x

ghci> 22.4 / (fromIntegral x)
2.2399999999999998
ghci>
 
1793681466259.png

I see this as an absolute win. For the first time they can fix over 30 years of human slop.

The article concludes:

1793681593458.png
 
View attachment 9806232

I see this as an absolute win. For the first time they can fix over 30 years of human slop.

The article concludes:

View attachment 9806244

While I'm leery of all the AI glazing, a release that foregoes new features that maybe 0.1% of users would have any use for for just fixing and improving all the code that's already there is never a bad thing, especially when you don't have product managers breathing down your neck who only care about new features that they can put into a bullet list and sell to customers.
 
And not even easy to debug (I forgot to update the pointer linking back from the next element upon inserting, 2-3 hrs to track it down until I broke down and asked a clanker)
Hey, I wrote the same kind of bug when I made mine.
:story:

I, however, was able to use Common Lisp's inspector to step through the data structure and see what was wrong, after I got frustrated enough. It's truly amazing how many bugs become shallow merely by letting the operator look into things usually hidden; it's also pathetic.
 
Can I just say: Fuck doubly linked lists. Some of the weirdest and most subtle bugs ever. And not even easy to debug (I forgot to update the pointer linking back from the next element upon inserting, 2-3 hrs to track it down until I broke down and asked a clanker)
Could be worse, you could try debugging a XOR linked list.
 
Hey, I wrote the same kind of bug when I made mine.
:story:

I, however, was able to use Common Lisp's inspector to step through the data structure and see what was wrong, after I got frustrated enough. It's truly amazing how many bugs become shallow merely by letting the operator look into things usually hidden; it's also pathetic.
It was pure C and gdb actually allowed me to look at all the elements in the list at once (I allocated the entire memory range ahead of time, then just emplaced them). I just kept overlooking that one pointer not being updated.

Could be worse, you could try debugging a XOR linked list.
That actually would've made the problem much easier to spot though. Actually, an xor list would've overall just been a better fit for the entire thing.
 
I'm really proud of my design for doubly-linked lists: A linked list in Lisp has a CAR and a CDR, so I added a CBR. :)

I still need to go back and adjust my circularity detection code, since it turns out one can discard the tortoise and hare algorithm there and instead maintain the first node given; either both ends terminate normally or the first node will appear again, which is fairly simple.
 
I'm really proud of my design for doubly-linked lists: A linked list in Lisp has a CAR and a CDR, so I added a CBR. :)

I still need to go back and adjust my circularity detection code, since it turns out one can discard the tortoise and hare algorithm there and instead maintain the first node given; either both ends terminate normally or the first node will appear again, which is fairly simple.
To be fair, Tortoise and Hare is an algorithm for detecting graph cycles. It's overkill for linked lists.
 
To be fair, Tortoise and Hare is an algorithm for detecting graph cycles. It's overkill for linked lists.
In Lisp, linked lists are made of CONS cells by the CONS function, so they're really just graphs. The key insight is that linked lists permit a structure like so: A -> B -> C -> D -> E -> F -> D -> .... A doubly-linked list can't express a sub-loop like this because each node knows the node pointing to it, so a perfect loop is the only remaining circularity possible.
 
It was pure C and gdb actually allowed me to look at all the elements in the list at once (I allocated the entire memory range ahead of time, then just emplaced them). I just kept overlooking that one pointer not being updated.


That actually would've made the problem much easier to spot though. Actually, an xor list would've overall just been a better fit for the entire thing.
Tell you what's much more easy to reason about and much more elegant and much more performant: intrusive linked lists

Having the object itself hold the pointers to next and previous items makes item lifetime much less hassle especially when moving items to different lists whilst being atomic.

I used an atomic intrusive singly linked list (rolls off the tongue) just the other day when implementing a kernel C runtime for the atexit handler. I didn't want to hold broad locks because you can end up with exit functions that when invoked recursively reinvoke the atexit holding the same lock for the exit function table. We have magic statics now with lazy exit registration for some reason which you have to think about..
Microsoft got bitten by that at some point in their implementation Link

Atomics saved the day in this instance. Although its very, very rare that my colleagues (or more realistically chatgpt) ever uses them correctly without some giant TOCTOU or ABA problem. We need a concurrency benchmark for AI.
 
So i've been learning cpp (and c too!) but when we get to memory management i can't manage to figure shit out. I've been recommended by a friend to try to get some experience in rust cause the languages are basically the same but with a borrow checker so that when i do get the time to figure out memory management i can actually have some idea of what is to be written. not sure if this is the right thing to do since i've heard both good things about the cpp vs rust debate and just got told that as long as you don't interact with the communities, both are pretty good languages
 
Rust is a really bad language to learn because it's designed by BDSM freaks who constantly change the language out from under you and expect you to thank them for it (because a person of gender "dominatrix" breaking into your home or office, pushing you to the ground, and crushing your testicles under xir's stiletto heel is a felony that will get him on the sex offender registry, while maliciously changing a language definition to make formerly-valid code stop compiling is the next-best thing that happens to be legal). The main trick, if you're using the standard library's heap allocation, is to decide, for every function argument and return value, whether the caller or the callee "owns" that block of memory and is responsible for freeing it when it's outlived its usefulness and, if a block is passed in as a parameter that takes ownership to not refer to it again after the call returns (as it may have already been freed).
 
Back
Top Bottom