Generating Church Numbers with Haskell, Clojure, OCaml, StandardML

A few further implementations, which are rather boring, since made using functional languages.

Haskell (Thanks to Rincewind and Lambdabot):

let church = (foldl (.) id .) . replicate

Clojure:

(defn churchZero [y] (fn [x] x))
(defn churchSucc [n] (fn [f] (fn [x] (f ((n f) x)))))
(defn getChurchNumber [n]
 (loop [i n, r churchZero]
  (if (= i 0) r (recur (- i 1) (churchSucc r)))))
(((getChurchNumber 10) inc) 0)

OCaml (Thanks to m^3, posted in a former comment):

type Church = {C : 'a.('a -> 'a) -> ('a -> 'a)};;

let  zero = {C = fun f x -> x};;
let  succ n = {C = fun f x -> f (n.C f x)};;

StandardML:

fun churchZero y = fn x => x;
fun churchSucc n f x = f (n(f)(x));
fun getChurchNumber 0 = churchZero | getChurchNumber n = churchSucc(getChurchNumber (n-1));
getChurchNumber 10 inc 0;

I’d be interested in an Implementation especially in Perl and XSLT if possible. Also in PostScript, it should be possible, iirc. If someone can do that, please do it.

3 Antworten zu Generating Church Numbers with Haskell, Clojure, OCaml, StandardML

  1. Scala:

    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))
    };

  2. Factor (a very interesting exercise indeed):

    : zero ( — quot )
    [ drop [ ] ] ;

    : succ ( quot — quot )
    [ dup ] prepose [ compose ] compose ;

    : get-church-numeral ( int — quot )
    zero swap [ drop succ ] each ;

Schreibe einen Kommentar

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

WordPress.com-Logo

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

Twitter-Bild

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

Facebook-Foto

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

Google+ Foto

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

Verbinde mit %s

%d Bloggern gefällt das: