# Tutorial by Examples

## Hello, World!

A basic &quot;Hello, World!&quot; program in Haskell can be expressed concisely in just one or two lines: main :: IO () main = putStrLn &quot;Hello, World!&quot; The first line is an optional type annotation, indicating that main is a value of type IO (), representing an I/O action which &quot;...

## Factorial

The factorial function is a Haskell &quot;Hello World!&quot; (and for functional programming generally) in the sense that it succinctly demonstrates basic principles of the language. Variation 1 fac :: (Integral a) =&gt; a -&gt; a fac n = product [1..n] Live demo Integral is the class of in...

## Fibonacci, Using Lazy Evaluation

Lazy evaluation means Haskell will evaluate only list items whose values are needed. The basic recursive definition is: f (0) &lt;- 0 f (1) &lt;- 1 f (n) &lt;- f (n-1) + f (n-2) If evaluated directly, it will be very slow. But, imagine we have a list that records all the results, fibs ...

## Getting started

Online REPL The easiest way to get started writing Haskell is probably by going to the Haskell website or Try Haskell 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 c...

## Primes

A few most salient variants: Below 100 import Data.List ( (\\) ) ps100 = ((([2..100] \\ [4,6..100]) \\ [6,9..100]) \\ [10,15..100]) \\ [14,21..100] -- = (((2:[3,5..100]) \\ [9,15..100]) \\ [25,35..100]) \\ [49,63..100] -- = (2:[3,5..100]) \\ ([9,15..100] ++ [25,35..100] ++ [49,63..1...

## Declaring Values

We can declare a series of expressions in the REPL like this: Prelude&gt; let x = 5 Prelude&gt; let y = 2 * 5 + x Prelude&gt; let result = y * 10 Prelude&gt; x 5 Prelude&gt; y 15 Prelude&gt; result 150 To declare the same values in a file we write the following: -- demo.hs module De...

Page 1 of 1