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
1
u/kredati Feb 11 '23
Roc uses automatic reference counting:
> That said, there are also cases where Roc has strictly more runtime overhead than languages like C, C++, Zig, and Rust do. The most costly is automatic memory management, which Roc implements using automatic reference counting.
Whether or not RC counts as GC is a matter of some debate. But Rust and C++ both have their versions of RC. (And yes, the OP said C, not C++ or Rust.)