English 中文(简体)
How does "Language Oriented Programming" compare to OOP/Functional in the real world
原标题:
  • 时间:2009-11-09 21:16:37
  •  标签:
  • f#
  • paradigms

I recently began to read some F# related literature, speaking of "Real World Functional Programming" and "Expert F#" e. g.. At the beginning it s easy, because I have some background in Haskell, and know C#. But when it comes to "Language Oriented Programming" I just don t get it. - I read some explanations and it s like reading an academic paper that gets more abstract and strange with every sentence.

Does anybody have an easy example for that kind of stuff and how it compares to existing paradigms? It s not just academic fantasy, isn t it? ;)

Thanks, wishi

最佳回答

F# has a few mechanisms for doing programming in a style one might call "language-oriented".

First, the syntax niceties (function calls don t need parentheses, can define own infix operators, ...) make it so that many user-defined libraries have the appearance of embedded DSLs.

Second, the F# "quotations" mechanism can enable you to quote code and then run it with an alternative semantics/evaluation engine.

Third, F# "computation expressions" (aka workflows, monads, ...) also provide a way to provide a type of alternative semantics for certain blocks of code.

All of these kinda fall into the EDSL category.

问题回答

Language oriented program (LOP) can be used to describe any of the following.

Creating an external language (DSL)

This is perhaps the most common use of LOP, and is where you have a specific domain - such as UPS shipping packages via transit types through routes, etc. Rather than try to encode all of these domain-specific entities inside of program code, you rather create a separate programming language for just that domain. So you can encode your problem in a separate, external language.

Creating an internal language

Sometimes you want your program code to look less like code and map more closely to the problem domain. That is, have the code read more naturally . A fluent interface is an example of this: Fluent Interface. Also, F# has Active Patterns which support this quite well.

I wrote a blog post on LOP a while back that provides some code examples.

In Object Oriented Programming, you try to model a problem using Objects. You can then connect those Objects together to perform functions...and in the end solve the original problem.

In Language Oriented Programming, rather than use an existing Object Oriented or Functional Programming Language, you design a new Domain Specific Language that is best suited to efficiently solve your problem.

The term language Oriented Programming may be overloaded in that it might have different meanings to different people.

But in terms of how I ve used it, it means that you create a DSL(http://en.wikipedia.org/wiki/Domain_Specific_Language) before you start to solve your problem.

Once your DSL is created you would then write your program in terms of the DSL.

The idea being that your DSL is more suited to expressing the problem than a General purpose language would be.

Some examples would be the make file syntax or Ruby on Rails ActiveRecord class.

I haven t directly used language oriented programming in real-world situations (creating an actual language), but it is useful to think about and helps design better domain-driven objects.

In a sense, any real-world development in Lisp or Scheme can be considered "language-oriented," since you are developing the "language" of your application and its abstract tree as you code along. Cucumber is another real-world example I ve heard about.

Please note that there are some problems to this approach (and any domain-driven approach) in real-world development. One major problem that I ve dealt with before is mismatch between the logic that makes sense in the domain and the logic that makes sense in software. Domain (business) logic can be extremely convoluted and senseless - and causes domain models to break down.

An easy example of a domain-specific language, mentioned here, is SQL. Also: UNIX shell scripts.

Of course, if you are doing a lot of basic ops and have a lot of overlap with the underlying language, it is probably overengineering.





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签