[Haskell-cafe] Re: "quoting" in Haskell

Rene de Visser Rene_de_Visser at hotmail.com
Mon Aug 27 15:05:01 EDT 2007


"Peter Verswyvelen" <bf3 at telenet.be> schrieb im Newsbeitrag 
news:46D2E7F1.9080905 at telenet.be...
> In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# 
> and C# 3.0 also have something similar that turns code into "expression 
> trees". The latter is used extensively in LINQ which translates plain C# 
> code into SQL code or any other code at runtime (this idea came from FP I 
> heared)

The normal way of doing such things in Haskell is to have
1) functions that generate the component data structures (these functions 
are often called smart constructors)
2) other functions to put the functions/data structures together (these 
other functions are often call combinators).

The resulting data structure that represents the sql query for example is 
then processed to produce the real (textual) sql query which this then sent 
to the database.
>
> I can't find something similar for Haskell? Maybe I am looking at the 
> wrong places?

HaskellDB for example does this for database queries.
Parsec does this parsers.
HSXML (if I got the name right) does this for XML.
>
> In Haskell, I know one can use a data constructor as a function (as in 
> (map Just [1..3])), but a function cannot be turned into a data 
> constructor (= "quoting"), can it?
A data constructor is a special case of a function, or perhaps better said, 
a particular way a function is defined. Either a function is a data 
constructor or it isn't.

For example you can also do

just = Just

Just is a data constuctor. It was defined with a data statement (and as a 
result starts with a capital letter).
data Maybe a = Nothing | Just a

just is not a data constructor. Why? It wasn't defined with a data 
statement.

However just and Just behave almost identically. (you can't pattern match on 
just, only on Just)

Rene. 





More information about the Haskell-Cafe mailing list