What tools are there for functional programming in C?

FFCALL lets you build closures in C — callback = alloc_callback(&function, data) returns a function pointer such that callback(arg1, …) is equivalent to calling function(data, arg1, …). You will have to handle garbage collection manually, though. Relatedly, blocks have been added to Apple’s fork of GCC; they’re not function pointers, but they let you pass around lambdas while avoiding the need to build … Read more

Reverse a list in haskell

You are separating the list into head and tail, but then re-assemble the list in the same order. Take the list [1, 2, 3] for example: In the first call, x will be 1, and xs will be [2, 3]. Then you create a new list, consisting of x (so 1) at the front, followed … Read more

What is the difference between procedural programming and functional programming?

A functional language (ideally) allows you to write a mathematical function, i.e. a function that takes n arguments and returns a value. If the program is executed, this function is logically evaluated as needed.1 A procedural language, on the other hand, performs a series of sequential steps. (There’s a way of transforming sequential logic into functional logic called continuation passing … Read more

What’s the difference between lapply and do.call?

There is a function called Map that may be similar to map in other languages: lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X. do.call constructs and executes a function call from a name or a function and a list of arguments … Read more

Haskell pattern matching – what is it?

In a nutshell, patterns are like defining piecewise functions in math. You can specify different function bodies for different arguments using patterns. When you call a function, the appropriate body is chosen by comparing the actual arguments with the various argument patterns. Read A Gentle Introduction to Haskell for more information. Compare: with the equivalent Haskell: Note … Read more