Difference between revisions of "Cookbook/Compilers and interpreters"

From HaskellWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
= Prelude =
 
= Prelude =
   
A lot of functions are defined in the [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Prelude]. Also, if you ever want to search for a function, based on the name, type or module, take a look at the excellent [http://www.haskell.org/hoogle/ Hoogle]. This is for a lot of people a must-have while debugging and writing Haskell programs.
+
A lot of functions are defined in the [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html 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 [http://www.haskell.org/hoogle/ Hoogle]. This is for a lot of people a must-have while debugging and writing Haskell programs.
   
 
= GHCi/Hugs =
 
= GHCi/Hugs =

Revision as of 09:13, 24 April 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 Standard 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