| Start |
|---|
| Hello, World! A basic "Hello, World!" program (opens new window) in Haskell can be expressed concisely in just one or two lines: The first line is an optional type annotation, indicating that Put this into a Executing the compiled file will result in the output Alternatively, The interactive REPL can also be used instead of compiling. It comes shipped with most Haskell environments, such as Alternatively, load scripts into ghci from a file using
Explanation:This first line is a type signature, declaring the type of Values of type Because Haskell has a fully-fledged Hindley-Milner type system (opens new window) which allows for automatic type inference, type signatures are technically optional: if you simply omit the This second line does the actual work: If you come from an imperative language, it may be helpful to note that this definition can also be written as: Or equivalently (Haskell has layout-based parsing; but beware mixing tabs and spaces inconsistently which will confuse this mechanism): Each line in a The The above is the same as defining You can look up the signature of
|
| Factorial The factorial function is a Haskell "Hello World!" (and for functional programming generally) in the sense that it succinctly demonstrates basic principles of the language. Variation 1
Variation 2This variation uses pattern matching to split the function definition into separate cases. The first definition is invoked if the argument is It is worth noting that, due to rewrite rules, both versions of |
Getting started Online REPLThe easiest way to get started writing Haskell is probably by going to the Haskell website (opens new window) or Try Haskell (opens new window) and use the online REPL (read-eval-print-loop) on the home page. The online REPL supports most basic functionality and even some IO. There is also a basic tutorial available which can be started by typing the command GHC(i)For programmers that are ready to engage a little bit more, there is GHCi, an interactive environment that comes with the Glorious/Glasgow Haskell Compiler (opens new window). The GHC can be installed separately, but that is only a compiler. In order to be able to install new libraries, tools like Cabal (opens new window) and Stack (opens new window) must be installed as well. If you are running a Unix-like operating system, the easiest installation is to install Stack (opens new window) using: This installs GHC isolated from the rest of your system, so it is easy to remove. All commands must be preceded by
These platforms can be installed by downloading an installer (opens new window) and following the instructions or by using your distribution's package manager (note that this version is not guaranteed to be up-to-date): Once installed, it should be possible to start GHCi by invoking the possibly with some more information on what libraries have been loaded before the Because writing the same things again and again on a single line is not always that practically, it might be a good idea to write the Haskell code in files. These files normally have As mentioned earlier, GHCi is a part of the GHC, which is actually a compiler. This compiler can be used to transform a this will create object files and an executable if there were no errors and the More advanced tools
|
| Fibonacci, Using Lazy Evaluation Lazy evaluation means Haskell will evaluate only list items whose values are needed. The basic recursive definition is: If evaluated directly, it will be very slow. But, imagine we have a list that records all the results, Then This is coded as: Or even as
Another way of writing
Thanks to lazy evaluation, both functions define infinite lists without computing them out entirely. That is, we can write a |
| Primes A few most salient variants: Below 100UnlimitedSieve of Eratosthenes, using data-ordlist package (opens new window): Traditional(a sub-optimal trial division sieve) Optimal trial divisionTransitionalFrom trial division to sieve of Eratosthenes: The Shortest Code
|
| Declaring Values We can declare a series of expressions in the REPL like this: To declare the same values in a file we write the following: Module names are capitalized, unlike variable names. RemarksHaskell (opens new window) is an advanced purely-functional programming language. Features:
The latest standard of Haskell is Haskell 2010. As of May 2016, a group is working on the next version, Haskell 2020. The official Haskell documentation (opens new window) is also a comprehensive and useful resource. Great place to find books, courses, tutorials, manuals, guides, etc. |
Read Article
https://develop.horje.com/learn/1434/reference