Here's a puzzle: define some higher-order function f
such that, using only f
and parentheses, you can define the three higher-order functions const
, id
and bothOn
.
Those three functions could be defined in a straightforward way and in the Haskell programming language as follows:
-- takes two arguments, ignores the second and returning the first"
const a _ = a
-- takes one argument returns it:
id a = a
-- takes three arguments: the first is applied to the third and the result of applying
-- the second to the third:
bothOn f g x = f x (g x)
You're limited to your choice of programming language here of course (lisp or haskell would be fine choices even if you don't know either).
And those of you who know what I'm up to (you know who you are) should only submit answers they thought up themselves ;-)
EDIT: Forgot to mention, the answer with the definition for f
with fewest characters wins. Also show how you defined the three functions in terms of f
.
EDIT: Well look at me with egg on my face. It was only after posting this question and trying to implement some known solutions to this problem in haskell that I realized that a statically-typed language isn't particularly well-suited to the untyped lambda calculus.
The infinite type errors... I could not make them stop. But I would be really interested if any haskell type system gurus out there can make a working implementation.
So for those who are still wondering, I was trying to trick you into defining a single-combinator base: essentially, in my understanding, a single function that is sufficient to be a turing complete system.
The three functions you were asked to define with your function were taken from the SKI Combinator calculus, which is a combinator base consisting of only three combinators. So by defining a single combinator (function) which is capable of defining S and K (I can be derived from S and K) you can easily prove that you have a 1-combinator base.
Here is a paper that goes over the derivation of one of the several known single-base combinators. And here is an interesting pair of esoteric languages Iota and Jot which is built on a single combinator and consists of only two symbols.
I'm thinking they should ask me to write the problem for the ICFP contest next year...