Click here to Skip to main content
12,300,607 members (23,136 online)
Click here to Skip to main content

Stats

123.2K views
3K downloads
244 bookmarked
Posted

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012 MIT
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.

License

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

Share

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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid