Haskell
Haskell (pronounced /hæskəl/) is a programming language multi-purpose standardized, functionally pure, with loose and memorized evaluation, and strong static typing. Its name is due to the American logician Haskell Curry, due to his contribution to lambda calculus, which has a great influence on language. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the primary control constructor is the function. The language has its origins in the observations of Haskell Curry and the intellectual descendants of him.
In the 1980s, a committee was formed whose objective was to create a functional language that would bring together the characteristics of the multiple functional languages of the time, most notably Miranda, and resolve the confusion created by their proliferation.
The language evolves rapidly and (see below) as the current representatives of the de facto standard. The latest official standard is: Haskell 2010, whose differences from the previous Haskell 98 standard are:
New language features:
- Interface of foreign functions (FFI), which allows you to use code C in a Haskell program and Haskell code in a program C. An explanatory example can be found here
- Hierarchical names for modules, for example Data.Bool.
- Guardians with patterns.
Removed language features:
- Pattern syntax (n+k). Thus, the following definition of the factorial function is not valid in Haskell 2010 and later:
fact (n+1) = (n+1) * fact n
.
The most interesting features of Haskell include support for recursive data types and functions, lists, tuples, guards, and pattern matching. The combination of these can result in some almost trivial functions whose version in imperative languages can become extremely tedious to program. Haskell is, since 2002, one of the functional languages on which more research has been carried out. Many variants have been developed:
- Parallel versions of MIT and Glasgow, both called Parallel Haskell.
- More parallel and distributed versions of Haskell calls Distributed Haskell (formerly) Goffinand Eden.
- A version with speculative execution: Eager Haskell.
- Various object-oriented versions: Haskell++, O'Haskell and Mondrian.
- An educational version called Gofer developed by Mark Jones that was supplanted by Hugs (see below).
For more detailed information, refer to the official site or to the links at the end of this article.
History
Since Miranda's publication in 1985, functional languages proliferated. In 1987, there were more than a dozen non-strict functional pure programming languages competing with each other. During the conference on Functional Programming Languages and Computer Architectures (FPCA '87) in Portland, Oregon, a meeting was held during which a strong consensus was reached among its participants to form a committee to define an open standard for such languages. This was done for the express purpose of consolidating the existing languages into a single one that would serve as a foundation for future research in language design. The first version of Haskell ("Haskell 1.0") was defined in 1990. The committee's efforts resulted in a series of definitions of the language, culminating in late 1997 in Haskell 98, which was intended to be a minimal, stable, and portable version of the language, along with a library associated standard for teaching, and as a basis for future extensions. The committee expressly approved the creation of extensions and variants of Haskell 98 by adding and incorporating experimental features.
In January 1999, the Haskell 98 language standard was published in "The Haskell 98 Report". In January 2003, a revised version was published in "Haskell 98 Language and Libraries: The Revised Report". The language continues to evolve rapidly, with implementations by Hugs and by GHC (see below), which they represent the current de facto standard. In early 2006, the process began to define a successor to the Haskell 98 standard, informally called Haskell′ ("Haskell Prime"). This process is intended to produce a minor revision of Haskell 98. In 2010 Haskell 2010 is released.
Predefined simple types
In Haskell, and in the following, "o:: t" means that the expression "or" es has type "t", and "t -> s" is a type, specifically a function, that consumes something of type "t" and produces something of the type "s". The operator (->) associates to the right, since "t -> s -> r" means "t -> (s -> r)".
The Bool type
Values with this type represent logical expressions whose result can be either True or False.
Functions and operators
(&&):: Bool -> Bool -> Bool
. Logical conjunction.(||):: Bool -> Bool -> Bool
. Logical disjunction.not:: Bool -> Bool
. Logical denial.otherwise:: Bool
. Constant function that returns the value True.
The type Int
Values of this type are limited-precision integers that cover at least the interval [-2^29, 2^29 - 1] ([minBound, maxBound]).
The Integer type
Values of this type are unlimited-precision integers that have the same functions and operators as the Int type.
The Float type
Values of this type are real numbers. (2010, 23.4, 5.7)
Functions and operators
(+), (-), (*), (/), (^):: Float -> Float -> Float
. Add, subtraction, product, actual division and full exponent power.abs, signum, negate:: Int -> Int
. Absolute value, sign and denial.(**):: Float -> Float
. Power of real exponent
The Double type
Values of this type are real numbers, with a higher range and more precise approximations than those of the Float type.
The Char type
Values of this type are characters found in a mass of high complexity of in a sum of given characters with their high definition.
Before using this function in hugs we must use IMPORT CHAR before our algorithm.
Tuples
The elements that make up a tuple can be of the same or different types. It is a set of related components. For example: ('a', True,3)
Lists
Values of this type are a collection of elements of the same type. There are two constructors for lists:
- [Elements_separated_by_comas], For example:
[1,2,3,4]
- (first_element:rest_of_the_list), For example:
(1:(2:(3:(4:[]))))
Implementations
All of the following implementations are fully, or nearly fully, compliant with the Haskell 98 standards and are distributed under Open Source licenses. There are no known commercial implementations of the language.
- Hugs ([1]) is an interpreter. It offers a quick compilation of programs and a reasonable time of execution. It also comes with a very simple graphic library, which makes it suitable for those who are learning it. However it is not an implementation to despise, it is one of the lightest and most compatible.
- GHC ([2]): "Glasgow Haskell Compiler" compiles native code into a variety of architectures and can also compile C. It is probably one of the most popular compilers and even has a few libraries (e.g. OpenGL) that, although very useful, only run under GHC.
- nhc98 ([3]) is another compiler with a better run time than Hugs. This implementation focused on minimizing the use of memory by making it a good choice for slow or old architectures.
- HBC ([4]) is another Haskell native code compiler. Although it has not been updated in the last time it remains quite useful.
- Helium ([5]) is a new Haskell dialect. It focused on being very easy to learn; therefore it does not include support for the entire Haskell standard, making it not fully compatible.
Development environments
There are several IDEs (Integrated Development Environment, or in Spanish, integrated development environments) and/or plugins that can be used to develop applications in Haskell. Most are open source, but they also exist under a commercial license.
Open Source
- IntelliJ plugin for Haskell
- EclipseFP plugin for Eclipse IDE
- Colorer plugin for Eclipse IDE
- Leksah
- KDevelop
- Vim
- Emacs
- Atom
Commercials
- Haskell for Mac
- Sublime-Haskell
Web Frameworks
The Haskell web development community has recently had a huge boost in activity, resulting in a plethora of library choices to use. Below is the list of active Haskell frameworks for web development.
Happstack
It is designed so that developers can prototype quickly, deploy painlessly, scale massively, operate reliably, and change easily. It supports GNU/Linux, macOS, FreeBSD and Windows environments.
Considered a complete web framework, the main component is happstack-server: an integrated HTTP server, routing and fileserving combiners. Additionally, a number of packages that used to be coupled with Happstack have now been decoupled from it, but are still promoted and documented for use with Happstack:
- Safecopy: serialization support and migration of data types
- Acid-state: a NoSQL ACID storage system with native support for Haskell types
Snap
It is a web development framework based on a snaplet architecture.
It is fully documented and has a test suite with a high level of code coverage, but it is early stage software with constantly evolving interfaces
Yesod
It is designed for high-performance, secure, RESTful web applications.
Leveraging quasi-quoting for the easiest tasks, it provides concise web applications with high levels of security. Hamlet templates are checked at compile time for correctness, and the controller uses safe URLs to ensure that it is only generating valid URLs. It follows the principles of Model-View-Controller loosely.
Miku
A simple library for rapid web prototyping in Haskell, inspired by Ruby's Rack and Sinatra.
Lemmachine
It is a RESTful web framework. The main architecture is an Erlang-based copy of Webmachine, which is currently the best documentation reference. It is noted for its dynamic Webmachine written in a dependent-typed manner in Agda.
Mohws
It is a web server with a module system and support for CGI. Based on Simon Marlow's original Haskell Web Server.
Sage
It is a web server and web application framework that can be used to write dynamic websites in Haskell. From the lower level protocol code to the high level application code, everything is written as a Salvia controller. This approach makes the server extremely extensible.
Scotty
It's a Sinatra Ruby-inspired web framework, using WAI and Warp. It is an easy way to write RESTful and declarative web applications. It has good documentation for all the relevant functions.
Servant
It is a lightweight framework primarily for RESTful APIs. It allows you to specify the specifications of an API as type aliases and then work with these type aliases to create servers, documentation, client code in Haskell and Javascript. It is based on WAI.
MFow
It is a web application server. MFlow is an abbreviation for "Message Flow". It is a continuation based framework, instead of other continuation based frameworks like Ocsigen (Ocaml), Coccoon (javascript) or Seaside (Smalltalk), it relies on a backtracking monad that maintains execution state synchronization with user navigation. Since the discontinuation of WASH, MFlow is the only continuation-style framework written in Haskell to date.
Uses standard and/or technical Haskell web libraries: WAI, Warp, Blaze HTML, HSP. Its core is the server and independent rendering. A class of extended formlets are used to create self-contained components, called widgets. They have formatting, AJAX, and server code. They can be composed to create the user interface.
Spock
It is a framework oriented to rapid web development: It offers everything you need to quickly start web hacking with Haskell: routing, middleware, JSON, blaze, sessions, cookies, database helper, csrf protection, global state.
Wheb
It is a WAI framework for creating robust, high concurrency, simple and effective web applications. Its primary goal is to extend the functionality of the WAI library base and provide an easy entry point into Haskell web servers. Facilitates the creation of plugins. Plugins can add routes, middleware, configurations, and even handle resource cleanup on server shutdown. Named routes allow plugins to dynamically generate their routes at runtime based on configuration
WebAPI
WebApi is a lightweight WAI-based library that allows you to define requests and responses for endpoints as types through a contract. The contract is considered the only source of truth, with which WebApi allows to create Web services / REST APIs, generate a Haskell client for the existing API services, generate a mock server to simulate the requests and responses.
DAML
It is a smart contract framework based on the Glasgow Haskell Compiler.
Community
The Haskell Symposium
"The Haskell Symposium" is an annual symposium organized by the International Conference on Functional Programming (ICFP). The symposium is sponsored by the Association for Computing Machinery (ACM), under the auspices of the ACM Special Interest Group on programming languages (SIGPLAN).
The purpose of the event is to discuss programmers' experience with Haskell, and the future development of the language. The scope of the symposium includes all aspects of Haskell design, semantics, theory, application, implementation, and teaching.
Prior to 2008, the event was known as "The Haskell Workshop". The name change reflects the event's steadily increasing influence in the community at large, as well as an increasing number of high-quality submissions that make the acceptance process very competitive.
The Summer of Haskell
"The Summer of Haskell" is an event organized by haskell.org to reach out to students and encourage them to contribute to the Haskell community with the help of experienced mentors. This program is open to university students, over the age of 18, in most countries.
Examples
Recursive function to calculate the factorial of a natural number:
-- Recursive function that calculates the factorial of a natural number factorial :: Integer - 2005 Integer factorial n 日本語 n . 0 = error "there is no factorial for negative integers" 日本語 n ♪ 0 = 1 日本語 otherwise = n ♪ factorial (n-1)
Another version of the function to calculate the factorial of a natural using the product function of the Data.List module:
--Function to calculate the factorial of an integer using the product function of the Data.List module factorial :: Integer - 2005 Integer factorial n 日本語 n . 0 = error "there is no factorial for negative integers" 日本語 otherwise = product [chuckles]1..n]
Summation function of the elements of a list of integers
- Add items from a list sumar :: [chuckles]Int] - 2005 Int sumar [] = 0 sumar (x:xs) = x+sumar(xs)
Function to calculate the roots of a quadratic equation from its coefficients
--Function to calculate the roots of a second-degree equation from its coefficients roots :: Float - 2005 Float - 2005 Float - 2005 (Float, Float) roots a b c 日本語 disc ▪ 0 = (-b + rootDisc) / denomination, (-b - rootDisc) / denomination) 日本語 otherwise = error "The equation has complex roots" where disc = b♪b - 4♪a♪c rootDisc = sqrt disc denomination = 2♪a
Function that approximates the number e
--Function to calculate the value of e (2,71828182845905) euler :: Double - 2005 Double euler 0.0 = 1.0 euler n = 1.0 / product [chuckles]1..n] + euler (n - 1.0)
Function to calculate the greatest common divisor of two integers using Euclid's algorithm
mcd::Int- 2005Int- 2005Int mcd x 0 = x mcd x and = mcd and (mod x and)
Function performed by Eratosthenes' sieve (from a given list it leaves only the prime numbers)
Eratosten :: [chuckles]Int] - 2005 [chuckles]Int] Eratosten [] = [] Eratosten (x:xs) 日本語 not (null xs) " fake " x^2 ▪ last xs = (x:xs) 日本語 otherwise = x: Eratosten [chuckles]and 日本語 and . xs, and `mod` x ♪ 0]
Function that determines if a natural number is palindrome (in base 10)
isPalindrome :: Int - 2005 BoolisPalindrome n 日本語 n . 0 = False 日本語 otherwise = Let's go. s = show n in s ♪ (reverse s)
Function that performs the power of a number with recursion by dividing the file by 2
power :: Double - 2005 Int - 2005 Double power b e = if e . 0 then 1 / (power b (-e) else if e ♪ 0 then 1 else Let's go. p = power b (e `div` 2) in if e `mod` 2 ♪ 1 then p ♪ p ♪ b else p ♪ p
Function that lazily evaluates the list of prime numbers using a where clause and intentional lists
NumbersPrimos::[chuckles]Integer]NumbersPrimos = 2 : [chuckles]p 日本語 p . [chuckles]3 ..], It's first p] where It's first n 日本語 even n = False 日本語 otherwise = null [chuckles]m 日本語 m . [chuckles]3, 5 .. div n 2], mod n m ♪ 0]
Implementation of the Quicksort algorithm
qsort :: (Ord a) = 2005 [chuckles]a] - 2005 [chuckles]a]qsort [] = []qsort (x:xs) = qsort [chuckles]and 日本語 and . xs, and ♫ x] + [chuckles]x] + qsort [chuckles]and 日本語 and . xs, and ▪ x]
Contenido relacionado
Elevator
Thermoionic valve
Computer cluster