Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012
An introduction to creating programming language tools using C# 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Diggins.Jigsaw
    public class EmbeddedScheme
        public delegate dynamic Function(params object[] args);

        public static Function Lambda(DynamicFunc0 f) { return (args) => f(); }
        public static Function Lambda(DynamicFunc1 f) { return (args) => f(args[0]); }
        public static Function Lambda(DynamicFunc2 f) { return (args) => f(args[0], args[1]); }
        public static Function Lambda(DynamicFunc3 f) { return (args) => f(args[0], args[1], args[2]); }

        public static void Tests()
                Function f = Lambda((a, b) => a + b);
                Console.WriteLine(f(1, 2));

            // The self-applicative combinator
            Function M = Lambda(f => f(f));

                // Factorial written as a fixed-point. 
                Function fact = M(Lambda(f => Lambda(n => n <= 1 ? 1 : n * M(f)(n - 1))));

                for (int i = 1; i < 10; ++i)
                    Console.WriteLine("Factorial of {0} is {1}", i, fact(i));

            // Question: how can I implement the "letrec" in embedded Scheme? 
            // Question: how can I implement "define".
            // Todo: Note the cheats

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid