Difference between revisions of "Introduction.es"

From HaskellWiki
Jump to navigation Jump to search
 
Line 1: Line 1:
Haskell es un lenguaje de programación para computadoras. Específicamente,
 
es un lenguaje '' polimórficamente tipificado, peresozo, puramente funcional '', muy diferente a la mayoría de los otros lenguajes de programación.
 
 
El lenguaje recibe su nombre en honor a [[Haskell Brooks Curry]], por sus trabajos en lógica matemática que sirvieron como fundamento para el desarrollo de lenguajes funcionales. Haskell está basado en el '' cálculo lambda '', por lo tanto el símbolo lambda es usado como logo.
 
 
 
==¿Porqué usar Haskell?==
 
 
Escribir programas grandes que funcionen correctamente es difícil y costoso. Mantener esos programas es aún más difícil y costoso tambien. Los lenguajes de programación funcional, tales como Haskell, pueden hacerlo mucho más fácil y económicos. Por ejemplo, un usuario nuevo quién escribió una pequeña DBMS relacional en Haskell comentó lo siguiente:
 
<blockquote>
 
WOW! Yo básicamente escribí esto sin probarlo, sólo pensando acerca de mi programa en términos de transformaciones entre tipos. Escribí el código de ejemplos/pruebas y no tuve casi ningun error de implementación en el código!. El compilador/sistema-de-tipos es realmente realmente bueno en evitar que cometas errores de código!. Yo nunca habia tenido en mi vida un bloque de código tan grande que funcionara en el primer intento. Estoy absolutamente impresionado.
 
</blockquote>
 
Incluso si tú no estas en una posición para usar Haskell en tus proyectos de programación, aprender Haskell puede hacerte un mejor programador en cualquier lenguaje.
 
<blockquote>
 
Yo aprendí Haskell un par de años atrás, habiendo previamente programado en Python y (muchos) otros lenguajes. Recientemente he estado usando Python para un proyecto (la elección ha sido determinada tanto por aspectos técnicos y no-técnicos), y me doy cuenta que mi estilo de programar en Python ha sido ampliamente influenciado (para mejor, espero ;-) por mi experiencia de programación en Haskell.<br><br>
 
Graham Klyne
 
</blockquote>
 
 
Haskell te ofrece:
 
<ul>
 
* Un incremento substancial de productividad para el programador (Ericsson alcanzó un factor de mejoría entre 9 y 25 usando Erlang, un lenguaje de programación funcional similar a Haskell, en uno de sus experimentos en programas de telefonía).
 
* Código más corto, claro y fácil de mantener.
 
* Menos errores, mayor confiabilidad.
 
* Menor diferencia semántica; entre el programador y el lenguaje.
 
* Menor tiempo de análisis.
 
</ul>
 
Haskell es un lenguaje de un amplio espectro, conveniente para una gran variedad de aplicaciones. Específicamente para programas que necesitan ser fáciles de modificar y de mantener.
 
 
Gran parte de la vida de un programa se invierte en ''especificación'', ''diseño'' y ''mantenimiento'', y no en ''programación''. Los lenguajes funcionales son magníficos para escribir especificaciones que de hecho puedan ser ejecutadas (y por lo tanto probadas y depuradas). Tal especificación ''es'' el primer prototipo del programa final.
 
 
Los programas funcionales son también relativamente fácil de mantener, porque el código es más corto, claro, y el riguroso control sobre efectos colaterales (''side-effects'') elimina una amplia clase de interacciones imprevisibles.
 
 
===¿Qué es la programación funcional?===
 
C, Java, Pascal, Ada, y la gran mayoría de los lenguajes, son todos ''imperativos''. Ellos son &quot;imperativos&quot; en el sentido de que consisten en una secuencia de comandos, los cuales son ejecutados estrictamente uno después de otro. Haskell es un lenguaje ''funcional''. Un programa funcional es una expresión, la cual es ejecutada mediante su evaluación.
 
 
Cualquiera que haya usado una hoja de cálculo tiene experiencia en programación funcional. En una hoja de cálculo, se especifíca el valor de cada celda en términos de los valores de otras celdas. El enfoque es ''que'' se debe computar, no en ''cómo'' debería ser computado. Por ejemplo:
 
 
<ul>
 
* No se especifica el orden en el cual las celdas deberían ser calculadas - en cambio, confiamos que la hoja de cálculo evaluará las celdas en un orden que respete sus dependencias.
 
* No le decimos a la hoja de cálculo cómo reservar su memoria - en cambio, esperamos que nos presente un plano aparentemente infinito de celdas, y en dónde se reserve memoria sólo para esas celdas que esten usándose.
 
* Nosotros especificamos el valor de una celda mediante una ''expresión'' (cuyas partes pueden ser evaluadas en cualquier orden), en lugar de mediante una ''secuencia de comandos'' que computen su valor.
 
</ul>
 
 
Una interesante consecuencia de no especificar el orden de evaluación en la hoja de cálculo es que la noción de ''asignación'' no es muy útil. Después de todo, si no sabes exactamente cuando una asignación sucederá, no puedes hacer mucho uso de ella!. Esto contrasta fuertemente con programas escritos en lenguajes convencionales como C, los cuales consisten esencialmente de una secuencia cuidadosamente especificada de asginaciones, ó Java, en el cual el orden de las llamadas a métodos es crucial para el significado de un programa.
 
 
Esto se enfoca en el ''alto-nivel'' &quot;qué&quot; en lugar del ''bajo-nivel'' &quot;cómo&quot; ; esta es una característica distintiva de los lenguajes de programación funcional.
 
 
Otro lenguaje cercanamente funcional bien conocido, es el lenguaje estándar de búsqueda de base de datos SQL. Una búsqueda SQL es una expresión que involucra proyecciones, selecciones, uniones y así sucesivamente. La búsqueda especifica qué relación debería de ser computada, sin especificar cómo debería ser computada. La búsqueda puede ser evaluada en cualquier orden. Las implementaciones de SQL a menudo ejecutan extensas optimizaciones de búsqueda las cuales (junto a otros factores) calculan el mejor orden para evaluar la expresión.
 
 
 
===What's good about functional programming?===
 
 
Spreadsheets and SQL are both fairly specialized languages. Functional
 
programming languages take the same ideas, and move them into the realm
 
of general-purpose programming. To get an idea of what a functional
 
program is like, and the expressiveness of functional languages, look at
 
the following quicksort programs. They both sort a sequence of numbers
 
into ascending order using a standard method called "quicksort". The
 
first program is written in Haskell and the second in C.
 
 
Whereas the C program describes the particular steps the machine must
 
make to perform a sort -- with most code dealing with the low-level
 
details of data manipulation -- the Haskell program encodes the sorting
 
algorithm at a much higher level, with improved brevity and clarity as
 
a result.
 
 
====Quicksort in Haskell====
 
 
<haskell>
 
qsort [] = []
 
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
 
</haskell>
 
 
====Quicksort in C====
 
 
<pre>
 
void qsort(int a[], int lo, int hi) {
 
{
 
int h, l, p, t;
 
 
if (lo &lt; hi) {
 
l = lo;
 
h = hi;
 
p = a[hi];
 
 
do {
 
while ((l &lt; h) &amp;&amp; (a[l] &lt;= p))
 
l = l+1;
 
while ((h &gt; l) &amp;&amp; (a[h] &gt;= p))
 
h = h-1;
 
if (l &lt; h) {
 
t = a[l];
 
a[l] = a[h];
 
a[h] = t;
 
}
 
} while (l &lt; h);
 
 
t = a[l];
 
a[l] = a[hi];
 
a[hi] = t;
 
 
qsort( a, lo, l-1 );
 
qsort( a, l+1, hi );
 
}
 
}
 
</pre>
 
 
Let's examine some of benefits of Haskell and functional programming.
 
A more detailed case for functional programming can be found in
 
 
<BLOCKQUOTE>
 
[http://www.md.chalmers.se/~rjmh/Papers/whyfp.html <STRONG>Why Functional Programming Matters</STRONG>] by [http://www.md.chalmers.se/~rjmh/ John Hughes], The Computer
 
Journal, Vol. 32, No. 2, 1989, pp. 98 - 107. Also in: David A. Turner
 
(ed.): Research Topics in Functional Programming, Addison-Wesley,
 
1990, pp. 17 - 42.
 
</BLOCKQUOTE>
 
A slightly less formal essay inspired by the paper above can be found in
 
<BLOCKQUOTE>
 
[[Why Haskell Matters |<STRONG>Why Haskell Matters</STRONG>]] originally by [mailto:sylvan@dtek.chalmers.se Sebastian Sylvan]
 
</BLOCKQUOTE>
 
 
====1. Brevity====
 
Functional programs tend to be much more <B>concise</B> than their
 
imperative counterparts. Quicksort is a rather extreme case, but in
 
general functional programs are much shorter (two to ten times).
 
 
====2. Ease of understanding====
 
Functional programs are often easier to '''understand'''. You should be able to understand the program without any previous knowledge of either Haskell or quicksort. The same certainly cannot be said of the C program. It takes quite a while to understand, and even when you do understand it, it is extremely easy to make a small slip and end up with an incorrect program. Here is a detailed explanation of the Haskell quicksort:
 
<haskell>
 
qsort [] = []
 
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
 
</haskell>
 
The first line reads:
 
"When you sort an empty list (<tt>[]</tt>), the result is another empty list".
 
The second line reads: "To sort a list whose first element is named <tt>x</tt> and
 
the rest of which is named <tt>xs</tt>, sort the elements of <tt>xs</tt> that are less than <tt>x</tt>, sort the elements of <tt>xs</tt> that are greater than or equal to <tt>x</tt>, and concatenate (<tt>++</tt>) the results, with <tt>x</tt> sandwiched in the middle."
 
 
====3. No core dumps====
 
Most functional languages, and Haskell in particular, are <B>strongly
 
typed</B>, eliminating a huge class of easy-to-make errors at compile
 
time. In particular, strong typing means <B>no core dumps</B>!
 
There is simply no possibility of treating an integer as a pointer, or
 
following a null pointer.
 
 
====4. Code re-use====
 
Of course, strong typing is available in many imperative languages, such as Ada or Pascal. However, Haskell's type system is much less restrictive than, say, Pascal's, because it uses '''polymorphism'''.
 
 
For example, the qsort program given in Figure 1 will not only sort lists of integers, but also lists of floating point numbers, lists of characters, lists of lists; indeed, it will sort lists of ''anything'' for which it is meaningful to have "less-than" and "greater-than" operations. In contrast, the C version can only sort an array of integers, and nothing else.
 
 
Polymorphism enhances re-usability.
 
 
====5. Strong glue====
 
Non-strict functional languages have another powerful feature: they
 
only evaluate as much of the program as is required to get the answer
 
- this is called <B>lazy evaluation</B>. This feature is rather
 
like Unix pipes. For example, the Unix command
 
<pre>
 
grep printf Foo.c | wc
 
</pre>
 
counts the number of lines in the file <tt> Foo.c </tt>which include the
 
string <tt>printf</tt>.
 
The command
 
<pre>
 
grep printf Foo.c
 
</pre>
 
produces all lines which contain the string &quot;<tt>printf</tt>&quot;,
 
while the &quot;<tt>wc</tt>&quot; command counts them. The pipe,
 
written &quot;<tt>|</tt>&quot;, takes the output from the first command
 
and delivers it to the second. The two commands execute together, so
 
that the output of the first is consumed more-or-less immediately by
 
the second. In this way, no large intermediate files need be
 
produced. You can think of <tt>wc</tt> &quot;demanding&quot;
 
lines from the <tt>grep</tt>
 
 
If the second command only needs some of the output of the first, then
 
execution of the first command might never need to be completed. For
 
example
 
 
<pre>
 
grep printf Foo.c | head 5
 
</pre>
 
just prints the first 5 lines which contain &quot;<tt>printf</tt>&quot;.
 
There is no need to modify the <tt>grep</tt> command to take account of
 
the fact that its execution might be abandoned.
 
 
Non-strict languages provide exactly this kind of demand-driven
 
evaluation. Data structures are evaluated just enough to deliver the
 
answer, and parts of them may not be evaluated at all. As in the case
 
of Unix commands, this provides powerful &quot;glue&quot; with which
 
to compose existing programs together. What this means is that it is
 
possible to <B>re-use programs</B>, or pieces of programs, much more
 
often than can be done in an imperative setting. Lazy evaluation
 
allows us to write more <B>modular programs</b>.
 
 
====6. Powerful abstractions====
 
In general, functional languages offer powerful new ways to
 
encapsulate <B>abstractions</B>. An abstraction allows you to define
 
an object whose internal workings are hidden; a C procedure, for
 
example, is an abstraction. Abstractions are ''the'' key to
 
building modular, maintainable programs, so much so that a good
 
question to ask of any new language is "what mechanisms for
 
abstraction does it provide?".
 
 
One powerful abstraction mechanism available in functional languages
 
is the <B>higher-order function</B>. In Haskell a function is a
 
first-class citizen: it can freely be passed to other functions,
 
returned as the result of a function, stored in a data structure, and
 
so on. It turns out that the judicious use of higher-order functions
 
can substantially improve the structure and modularity of many
 
programs.
 
 
<h4> 7. Built-in memory management</h4>
 
Very many sophisticated programs need to allocate dynamic memory from a heap. In C this is done with a call to <tt> malloc</tt>, followed by code to initialize the store just allocated. The programmer is responsible for returning the store to the free pool when it isn't needed any more, a notorious source of "dangling-pointer" errors. To make matters worse, <tt>malloc</tt> is fairly expensive performance-wise, so programmers often <tt>malloc</tt> a single large chunk of store, and then allocate "by hand" out of this.
 
 
Every functional language relieves the programmer of this storage management burden. Store is allocated and initialized implicitly, and recovered automatically by the garbage collector. The technology of storage allocation and garbage collection is now well developed, and the performance costs are rather slight.
 
 
<h3>When C is better</h3>
 
It isn't all roses, of course. The C quicksort uses an extremely
 
ingenious technique, invented by Hoare, whereby it sorts the array
 
''in place''; that is, without using any extra storage. As a
 
result, it runs quickly, and in a small amount of memory. In
 
contrast, the Haskell program allocates quite a lot of extra memory
 
behind the scenes, and runs rather slower than the C program.
 
 
In effect, the C quicksort does some very ingenious storage
 
management, trading this algorithmic complexity for a reduction in
 
run-time storage management costs.
 
 
In applications where performance is required at any cost, or when the
 
goal is detailed tuning of a low-level algorithm, an imperative
 
language like C would probably be a better choice than Haskell,
 
exactly because it provides more intimate control over the exact way
 
in which the computation is carried out.
 
 
====Functional vs imperative====
 
But few programs require performance at any cost! After all, we all
 
stopped writing assembly-language programs, except perhaps for key
 
inner loops, long ago. The benefits of having a more supportive
 
programming model (an arbitrary number of named, local variables
 
instead of a fixed number of registers, for example) far outweigh the
 
modest run-time costs.
 
 
Similarly, we willingly accept the costs of a virtual memory paging
 
system, in exchange for the more supportive programming model of an
 
infinite virtual address space. The days of explicit memory overlays
 
are over.
 
 
Functional languages take another large step towards a higher-level
 
programing model. Programs are easier to design, write and maintain,
 
but the language offers the programmer less control over the machine.
 
For most programs the result is perfectly acceptable.
 
 
===What is Haskell?===
 
Haskell is a modern, standard, non-strict, purely-functional
 
programming language. It provides all the features sketched above,
 
including polymorphic typing, lazy evaluation and higher-order
 
functions. It also has an innovative type system which supports a
 
systematic form of overloading and a module system.
 
 
It is specifically designed to handle a wide range of applications,
 
from numerical through to symbolic. To this end, Haskell has an
 
expressive syntax, and a rich variety of built-in data types,
 
including arbitrary-precision integers and rationals, as well as the
 
more conventional integer, floating-point and boolean types.
 
 
There are a number of [[Compilers and interpreters|compilers and interpreters]] available. All are
 
free. First-time users may want to start with [http://haskell.org/hugs/ Hugs], a small, portable Haskell interpreter.
 
 
See also [[History_of_Haskell|the History of Haskell]]
 
 
===Does Anyone Use Functional Programming?===
 
Functional programming languages are used in substantial applications.
 
For example:
 
<ul>
 
* Software AG, a major German software company, market an expert system (Natural Expert) which is programmed in a functional language. Their users find it easy to develop their applications in this language, through which they gain access to an underlying database system. It all runs on an IBM mainframe.
 
*Ericsson have developed a new functional language, Erlang, to use in their future telephony applications. They have already written 130k-line Erlang applications, and find them very much shorter and faster to develop.
 
*Amoco ran an experiment in which they re-coded in a functional language a substantial fraction of their main oil-reservoir simulation code, a critical application. The resulting program was vastly shorter, and its production revealed a number of errors in the existing software. Amoco subsequently transcribed the functional program into ... with encouraging results. <!-- Into what? Don't leave us hanging here. -->
 
*A researcher at the MITRE corporation is using Haskell to prototype his digital signal-processing applications.
 
*Researchers at Durham University used a functional language in a seven-year project to build LOLITA, a 30,000-line program for natural-language understanding.
 
*Query is the query language of the O2 object-oriented database system. O2Query is probably the most sophisticated commercially-available object-oriented database query language and it is a functional language.
 
*ICAD Inc market a CAD system for mechanical and aeronautical engineers. The language in which the engineers describe their design is functional, and it uses lazy evaluation extensively to avoid recomputing parts of the design which are not currently visible on the screen. This results in substantial performance improvements.
 
*An incestuous example: the Glasgow Haskell compiler is written in Haskell: a 30,000-line application.
 
</ul>
 
Some other examples of [[Haskell in practice]].
 
 
Clifford Beshers, of [http://www.linspire.com/ Linspire Inc]., describes their experience with Haskell, and functional programming:
 
 
<blockquote>
 
Linspire, Inc. has used functional programming since its inception in
 
2001, beginning with extensive use of O'Caml, with a steady shift to
 
Haskell as its implementations and libraries have matured. Hardware
 
detection, software packaging and CGI web page generation are all areas
 
where we have used functional programming extensively.
 
</blockquote>
 
 
<blockquote>
 
Haskell's feature set lets us replace much of our use of little
 
languages (e.g., bash or awk) and two-level languages (C or C++ bound to
 
an interpreted language), allowing for faster development, better code
 
sharing and ultimately faster implementations. Above all, we value
 
static type checking for minimizing runtime errors in applications that
 
run in unknown environments and for wrapping legacy programs in strongly
 
typed functions to ensure that we pass valid arguments.
 
</blockquote>
 
 
===Other frequently-asked questions===
 
''Is functional programming hard to learn?''
 
<blockquote>
 
Functional programming does require a change in perspective, which
 
some programmers find hard. But Ericsson's experience in training
 
programmers in Erlang is that most find the transition easy -
 
provided they take the training need seriously rather than assuming
 
that they can "pick it up on the day".
 
</blockquote>
 
''Aren't functional programs very slow?''
 
<blockquote>They used to be, perhaps 20 years ago. But the compilers
 
have long since caught up. Haskell programs run fast for all but the
 
most performance-demanding applications. At the time of writing, Haskell
 
compiled via GHC is in 2nd place (behind C) in the
 
[http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=all Great Language Shootout],
 
with other functional languages also ranked highly.
 
</blockquote>
 
 
''I already have a large application in C or C++; can I benefit from
 
functional programming without rewriting my whole system?''
 
<blockquote>
 
 
Haskell has been successfully integrated into existing applications in
 
a number of ways.
 
[http://www.haskell.org/hdirect/ HaskellDirect] is
 
an IDL (Interface Description Language) based tool that allows Haskell
 
programs to work with software components. Low level C/C++ interfaces
 
can be generated with
 
[http://www.haskell.org/greencard Green Card] or
 
[http://www.cse.unsw.edu.au/~chak/haskell/c2hs/ C->Haskell], allowing
 
tight integration between Haskell and C. These tools have been used
 
to build a large number of successful, mixed language systems.
 
</blockquote>
 
 
'' What libraries does Haskell support?''
 
<blockquote>
 
Many software libraries have been developed for Haskell. See the
 
[[Libraries and tools| list of Haskell libraries]] for a list of much of
 
what is available.
 
 
</blockquote>
 
 
'' What other software tools for Haskell are there? ''
 
<blockquote>
 
Glasgow Haskell comes with a profiler which allows you to find which
 
parts of your program are consuming most time and space. Chalmers
 
Haskell has a space-profiling tool, and a quasi-parallel simulator
 
which allows you to experiment with running your program in
 
parallel. Hugs also has some similar tools. For a complete list, check
 
the [[Libraries and tools|tools page]].
 
</blockquote>
 
 
''Can I get a support contract or a help-line?''
 
<blockquote>
 
It used to be the case that if you wanted help, you had to persuade a
 
Haskell research group that your problem was interesting enough or
 
important enough that they should spend time helping you for free.
 
<br>
 
Whilst that is still an option, there is now a
 
[[Consultants|directory of Haskell Consultants]] who provide:
 
 
<ul>
 
*Support for compilers, tools and libraries.
 
*Help with improving code quality (time, space, robustness, maintainability, etc.) using code reviews and tools.
 
*Help with using libraries, tools and advanced Haskell features such as type system extensions, exception handling, the foreign function interface, test harnesses, and concurrency.
 
*Library and application development.
 
*Staff training.
 
</ul>
 
These companies and individuals tend to work closely with those
 
developing Haskell (indeed, they have usually made major
 
contributions to Haskell themselves).
 
 
</blockquote>
 
''How can I learn Haskell?''
 
<blockquote>
 
For more example and explanations, look at the [http://www.haskell.org/tutorial/ Gentle Introduction to Haskell]. There are a
 
number of textbooks that use Haskell; see [[Books]].
 
</blockquote>
 
 
''Comparisons to other languages''
 
<blockquote>
 
A table comparing features of Haskell to similar languages is [[Comparison|here]].
 
</blockquote>
 
 
''Based on a paper by Simon Peyton Jones.''
 

Revision as of 13:45, 8 October 2006