background preloader

Programming, languages

Facebook Twitter

Overview for homoiconic. Ioke. What Made Lisp Different. December 2001 (rev. May 2002) (This article came about in response to some questions on the LL1 mailing list. It is now incorporated in Revenge of the Nerds.) When McCarthy designed Lisp in the late 1950s, it was a radical departure from existing languages, the most important of which was Fortran. Lisp embodied nine new ideas: 1. 2. 3. 4. 5. 6. It was natural to have this distinction in Fortran because (not surprisingly in a language where the input format was punched cards) the language was line-oriented. This limitation went away with the arrival of block-structured languages, but by then it was too late.

When a language is made entirely of expressions, you can compose expressions however you want. (if foo (= x 1) (= x 2)) or (= x (if foo 1 2)) 7. 8. 9. When Lisp was first invented, all these ideas were far removed from ordinary programming practice, which was dictated largely by the hardware available in the late 1950s. S-expression. An atom, oran expression of the form (x . y) where x and y are s-expressions. The second, recursive part of the definition represents an ordered pair so that s-exprs are effectively binary trees.

(x y z) stands for (x . (y . (z . Use in Lisp[edit] As noted above, the precise definition of "atom" varies across LISP-like languages. The recursive case of the s-expr definition is traditionally implemented using cons cells. S-expressions were originally intended only for data to be manipulated by M-expressions, but the first implementation of Lisp was an interpreter of S-expression encodings of M-expressions, and Lisp programmers soon became accustomed to using S-expressions for both code and data. Examples of data s-expressions[edit] Nested lists can be written as S-expressions: ((milk juice) (honey marmalade)) is a two-element S-expression whose elements are also two-element S-expressions. Example of source code s-expressions[edit] Example in Common Lisp: Standardization[edit] See also[edit] Five-Dollar Words For Programmers, Part Three: Homoiconic - Fabulous Adventures In Coding.

Hi Eric, I have two comments: 1) having a full AST in C# would be great. And I don't say this because it just sounds cool, but because it would enable better solutions for a number of problems we can currently only solve in very clumsy ways. However, I want to bring your attention to the problem of meta-programming in the static .NET type system in general. Even if we get AST-level access to an entire class, and be able to modify that AST (which is currently not possible, and copying ASTs is not always the better solution), we're still in a much worse position than any Lisp programmer would be. For two reasons: syntax and types. I don't need to tell you about syntax making meta-programming harder, and it's a problem that C# would share with many dynamic languages. "In ye olden times", the .NET type system was simple. It is not an accident that we found quite a few deep bugs in the .NET framework when we created a library that brings real mixins to C#.

(How could that look? Is that it? Homoiconic Languages. Languages in which program code is represented as the language's fundamental data type are called 'homoiconic'. Such languages allow code and data to be DeeplyIntertwingled, so that new code can be generated and manipulated by the program itself at runtime. See DefinitionOfHomoiconic Examples (in alphabetical order): Lisp specifies that code and data are both S-expressions, code S-expressions can be passed/returned/assigned to variables and have contents mutated, so code is FirstClass. The canonical example language is Lisp; both code and data are primarily represented in the same form: as trees (S-expressions) that are trivially and explicitly available for both unrestricted manipulation as data and also for unrestricted execution as code. Prolog is another example: A Prolog program is itself a sequence of Prolog terms that can be read, manipulated and evaluated using built-in mechanisms. Yes, but ... This would print "hello" 10 times, and then print "goodbye" 1000 times.

Homoiconicity. History[edit] The original source is the paper Macro Instruction Extensions of Compiler Languages,[2] according to the early and influential paper TRAC, A Text-Handling Language:[3] Alan Kay used and possibly popularized the term "homoiconic" through his use of the term in his 1969 PhD thesis:[4] A notable group of exceptions to all the previous systems are Interactive LISP [...] and TRAC. Both are functionally oriented (one list, the other string), both talk to the user with one language, and both are "homoiconic" in that their internal and external representations are essentially the same. They both have the ability to dynamically create new functions which may then be elaborated at the users's pleasure.Their only great drawback is that programs written in them look like King Burniburiach's letter to the Sumerians done in Babylonian cuniform! [...] Uses, advantages, and disadvantages[edit] A typical demonstration of homoiconicity is the meta-circular evaluator.

Examples[edit] Lisp code. 1 ? Meta Circular Evaluator. An evaluator that is written in the same language that it evaluates is said to be metacircular if and only if doing so short-circuits the need to specify the precise semantics, because the key language constructs are implemented by themselves, exactly like looking up a word in a dictionary and finding that the definition uses the original word. That's the "metacircular" part.

How is that different from ordinary recursion? It's circular recursion. There is no termination condition. It's a chicken-and-the-egg kind of thing. (There's actually a hidden termination condition: the bootstrapping process.) A C compiler written in C is not a MetaCircularEvaluator, because the compiler must specify extremely detailed and precise semantics for each and every construct. . : myInterpreter begin 32 word find dup if execute else number then again ; And here would be a compiler: : my-] begin 32 word find dup if dup immediate? And that should be it. Homoiconic Example In Many Programming Languages. Previous discussions of HomoiconicLanguages got bogged down in definitions. To cut this GordianKnot, let's define a concrete task that is easily implemented in a HomoiconicLanguage but impossible or difficult in other ProgrammingLanguages.

For starters, let's develop a spec from the LispLanguage example from HomoiconicLanguages. Create a "code" data structure (block) for assigning 15 to a variable Evaluate it and view the variable's contents (15) Modify that data structure to assign 37 to the variable Evaluate it and view the variable's contents (37) Optional: view the code blocks, if the environment allows it. Prohibited: Escaping to the OperatingSystem to save source files to disk or perform compilation Accessing language internals that are not portably defined in the language definition LispLanguage Here's a demonstration of what homoiconicity looks like in Lisp (one or more semicolons designates a comment): % set b 3 ;# Assign b the value 3 % puts $b ;# b is now 15 % $a.