Giter Club home page Giter Club logo

what-is-lisp-good-for-example's Introduction

This little example should demostrate what is Clojure and generally all Lisp dialects good for - building domain specific languages. It is motivated by now quite old but probably still relevant article written by Martin Fowler.

It also illustrates differences in an approach one might have trying to implement the solution in C# or Java (Fowler's article contains solution in C#), and in a Lisp (like Common Lisp or Clojure). Typically in C# one might have object-oriented approach of creating reusable classes (like ReaderStrategy and FieldExtractor) which are then instantiated for every different piece of input data. The other approach might take configuration out of C# and write it in an XML or configuration language with custom syntax.

Common Lisp implementation is done by Rainer Joswig, who described it here.

This very simple example can be used to illustrate to Java or C# programmers, used to class-based OOP style of (mostly) imperative programming, why they might sometimes, when complications in their implementation far exceed the complexity of the problem, consider using a language with syntactic abstractions, like Common Lisp or Clojure. Using macros one can extend base language, adding his own operators, possibly with custom evaluation rules and syntax.

There is more general point which this example illustrates, on a very basic, but also very vivid level, the one which SICP (there are some related quotes here) highlights as one of the main approaches of handling complexity - metalinguistic abstraction or building languages.

Crucial differences between Common Lisp and C# approach:

  1. In Lisp, code is data, so what in C# solution is written in configuration file, which is by itself not executable, can in Lisp be made to be executable via macros. "Putting parentheses around data" is approach taken in Joswig's solution. Whether configuration is done in XML or in some other data format, in C# solution there will be a need for some library which will help with reading that data. Approach without configuration file is more straightforward but makes extending the implementation with more data tedious and too distanced from the problem domain, since everything has to be expressed in C#.
  2. There is no need to create special classes which will hold data for each mapping, like in C# implementation. defclass will be executed for the name of each mapping.
  3. All the work done by ReaderStrategy and FieldExtractor classes from C# implementation, in Lisp implementation is done by parse-line-for-class method. It takes advantage of the fact that fields macro argument can be treated as list of lists and can be manipulated accordingly with destructuring, which makes this method very general.

Clojure additionally makes implementation more concise because it does not require definition of classes specifically for each mapping. Clojure's explicit aim is to avoid boilerplate code intrinsic to many OOP languages which require creation of specific classes for each piece of data, which are very often just maps (key-value pairs of slots and their values).

Generally, Lisp implementation is:

  1. More concise
  2. Easier to extend
  3. More general

So, in conclusion, it can be said that macros are a major win.

what-is-lisp-good-for-example's People

Contributors

lyssphacker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.