I’m looking into LINQ and the query language appears (at least on the surface) to be nothing more than an implementation of map and/or list comprehensions as found in Haskell and other FP languages (particularly the generalisation of ‘map’ and ‘for’ in Scala). Is this correct? Is there more to the syntax than this? From the breathless tone of the book I’m reading (“Essential LINQ”) it would seem like there’s something new or innovative here.
There’s the whole back-end, pipeline, first-order expression trees and types etc to implement LINQ but my question is about the query language itself.
Cheers
Joe
Functionally spoken, LINQ is nothing but a syntactic simplification of expressing monads. Linq to Objects (List-comprehensions – even this would already be extremely useful), which you have been talking about, is just one possible application of this (similar to the List-Monad in Haskell).
If you write
it’s nothing but
in Haskell.
The concrete thing that is done depends on user-defined Linq-providers (Extension-Methods) of which
Linq.Enumerableis just one implementation involvingIEnumerables.By providing one, you can create completely new LINQ-semantics for your types.
Example: Given an
Optiontype for computations that may fail (nullable values), one could define a Linq-provider for querying over them.This would now allow us to write such code:
The computation will only return a value if all computations succeeded and will otherwise fail at the first failing one.
In combination with expression trees, Linq can be extremely powerful and allows you to express –
Some links:
Combined with fixed-point combinators, Linq provides a complete functional mini-language (Linq raytracer).
Note that Scala and F# both have similar concepts in for-comprehensions and computation expressions both being monadic abstractions:
Scala:
F#: