r/functionalprogramming • u/Fabus1184 • 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?
View all comments
Show parent comment
4
u/josephjnk Feb 11 '23
That is a great post (and from what I can tell a pretty great blog.) I really liked this bit:
I have a recurring “argument” with a coworker wherein he says that assembly language is the only true reality in programming, since that’s what executes on the machine. I say that the true reality is the lambda calculus, because it describes the essential features that undergird our ability to write and understand programs.