r/functionalprogramming • • Feb 10 '23

Question Why is there no simple C-like functional programming language?

To be very clear, this means a compiled, statically-typed, non-garbage collected language with direct memory access and a fixed evaluation order that does not require any runtime system. I read multiple posts/comments saying it would be difficult or impractical. Still, I don't see why a declarative language wouldn't be able to solve that in an elegant way.

Think of something like this arbitrary Haskell-C-Mix:

doubleEach :: int32* -> int32 -> IO ()
doubleEach array 0 = pure ()
doubleEach array n = do 
    x <- readInt32 array
    writeInt32 array (x * 2)
    doubleEach (array + 1) (n - 1)

main :: IO ()
main = do
    array <- malloc (100 * sizeof int32)
    when (array == NULL) $ error "malloc failed"
    mapM_ (í -> writeInt32 (array + i) i) [0 .. 99]
    doubleEach array 100
    mapM_ (\i -> readInt32 (array + i) >>= print) [0 .. 99]
    free array

Such a language should be able to compile without a RTS or garbage collection and could be used virtually everywhere C is used. You could even make memory-safe programs eg. by using arrays with type-level lengths.

Admittedly there are a few gotchas like closures with references to stack-allocated variables and stuff, but nothing that couldn't be solved.

Why does this not exist, it seems like such a powerful language?

38 Upvotes

41 comments sorted by

View all comments

Show parent comment

4

u/editor_of_the_beast Feb 11 '23

And now you can tell them that both opinions are correct, but each is talking about a different dimension.

2

u/josephjnk Feb 11 '23

Yeah, I say “argument” because it’s in good fun and we both know that we’ll never convince the other, because neither of us are wrong. They’re statements of our philosophies rather than objective assertions.