Generating Church Numbers with Scala, Factor and Bash

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;
 while [ $i -le $(($argnum-3)) ]; do
  ret=`eval $fn $ret`;
 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!


2 Responses to Generating Church Numbers with Scala, Factor and Bash

  1. dasuxullebt sagt:

    I am working on one for x86 right now, but it doesnt work yet.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden /  Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )


Verbinde mit %s

%d Bloggern gefällt das: