2 ms·
I agree that the paragraphs about tables driven programs and function pointers are the best part. You can read "Combining datadriven programs with function po
by eru 16y ago
I agree that the paragraphs about tables driven programs and function pointers are the best part. You can read "Combining datadriven programs with function pointers leads to an astonishingly expressive way of working, a way that, in my experience, has often led to pleasant surprises." as much as an endorsement of functional languages than the endorsement of OO-like techniques it was meant to.
Though C is not really adequate to do Lisp-like programming. Because you can't capture free variables with C function pointers. Of course you can get around this restriction with lambda lifting [1], but why would you want to do the job of the compiler?
As an example, it is quite hard to encode function composition in C, i.e. write a function that takes two function and returns their composition. Or the same specification in (non-idiomatic [2]) Haskell:
composition :: (b -> c) -> (a -> b) -> a -> c
composition f g =
let h a = f (g a)
in h
I don't know of any way to make up new functions / function pointers in C at runtime.
[1] http://en.wikipedia.org/wiki/Lambda_lifting http://en.wikipedia.org/wiki/Lambda_lifting
[2] Idiomatic would be
composition :: (b -> c) -> (a -> b) -> a -> c
composition f g a = f (g a)
but that's harder to understand for a C programmer who's not familiar with currying. Of course Haskell also has composition as a build-in.
Or in Scheme (sorry, I am getting carried away with this):
(define ((composition f g) a)
(f (g a)))