The following two are written in Scala and Factor, two programming languages I didnt know so far. They have been posted in Comments to my last Blogpost about this topic, but I post them here again. I dont know what they actually do.
Scala (thanks to Matthias Benkard):
def zero[A](f: A => A)(x: A) = x;
def succ[A](n: (A => A) => A => A)(f: A => A)(x: A) = f(n(f)(x));
def getChurchNumber[A](x: Int): ((A => A) => A => A) = x match {
case 0 => zero[A] _
case n => succ(getChurchNumber[A](n - 1))
};
Factor (thanks to Matthias Benkard):
: zero ( -- quot ) [ drop [ ] ] ; : succ ( quot -- quot ) [ dup ] prepose [ compose ] compose ; : get-church-numeral ( int -- quot ) zero swap [ drop succ ] each ;
Well, and finally, a code from myself. An Implementation in Bash – at least as good as I could. After two days of handling around with Strings like $(lambda n “$(lambda f “$(lambda x \”\$(f \\\”\\\$(\\\\\\\”\\\\\\\$(n f)\\\\\\\” x)\\\”)\”)”)”) it was no fun anymore and so I did what I heard that bash-programmers always tend to do: I just hacked something together that does the job, but isnt beautiful in implementation. So if anybody has something better, feel free to send it.
#! /bin/bash
churchZero="dodo ";
churchSucc="dodo ";
dodo () {
local argnum=$#;
shift $((argnum-2));
local fn=$1;
shift 0;
local arg=$2;
i=0;
ret=$arg;
while [ $i -le $(($argnum-3)) ]; do
ret=`eval $fn $ret`;
((i++));
done
echo $ret;
}
incf () {
local a=$1;
echo $((a+1));
}
eval $churchSucc $churchSucc $churchSucc $churchSucc $churchSucc $churchZero incf 0
Further Sourcecode is welcome. Please produce it!
Verfasst von dasuxullebt 