Difference between revisions of "Cookbook/Compilers and interpreters"

From HaskellWiki
Jump to navigation Jump to search
(Change Prelude and Hoogle links to HaskellWiki pages)
(Change more Prelude links to HaskellWiki page)
Line 19: Line 19:
 
Prelude>
 
Prelude>
   
[http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Prelude] is the "base" library of Haskell.
+
[[Prelude]] is the "base" library of Haskell.
   
 
To create variables at the GHCi prompt, use `let'
 
To create variables at the GHCi prompt, use `let'
Line 49: Line 49:
 
"Hello" :: [Char]
 
"Hello" :: [Char]
 
</haskell>
 
</haskell>
Haskell has the following types defined in the [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Standard Prelude].
+
Haskell has the following types defined in the [[Prelude]].
 
<haskell>
 
<haskell>
 
Int -- bounded, word-sized integers
 
Int -- bounded, word-sized integers

Revision as of 23:24, 22 July 2009

Prelude

A lot of functions are defined in the Prelude. The Prelude is a standard module imported by default into all Haskell module.

Also, if you ever want to search for a function, based on the name, type or module, take a look at the excellent Hoogle. This is for a lot of people a must-have while debugging and writing Haskell programs.

GHCi/Hugs

GHCi interaction

To start GHCi from a command prompt, simply type `ghci'

   $ ghci
      ___         ___ _
     / _ \ /\  /\/ __(_)
    / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
   / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
   \____/\/ /_/\____/|_|      Type :? for help.
   
   Loading package base ... linking ... done.
   Prelude>

Prelude is the "base" library of Haskell.

To create variables at the GHCi prompt, use `let'

Prelude> let x = 5
Prelude> x
5
Prelude> let y = 3
Prelude> y
3
Prelude> x + y
8

`let' is also the way to create simple functions at the GHCi prompt

Prelude> let fact n = product [1..n]
Prelude> fact 5
120


Checking Types

To check the type of an expression or function, use the command `:t'

Prelude> :t x
x :: Integer
Prelude> :t "Hello"
"Hello" :: [Char]

Haskell has the following types defined in the Prelude.

    Int         -- bounded, word-sized integers
    Integer     -- unbounded integers
    Double      -- floating point values
    Char        -- characters
    String      -- equivalent to [Char], strings are lists of characters
    ()          -- the unit type
    Bool        -- booleans
    [a]         -- lists
    (a,b)       -- tuples / product types
    Either a b  -- sum types
    Maybe a     -- optional values