Cookbook/Compilers and interpreters: Difference between revisions

From HaskellWiki
(Change more Prelude links to HaskellWiki page)
(first step to turn this page from an arbitrary tutorial to a cookbook chapter)
Line 1: Line 1:
= Prelude =
== GHC ==
 
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'
<haskell>
Prelude> let x = 5
Prelude> x
5
Prelude> let y = 3
Prelude> y
3
Prelude> x + y
8
</haskell>
 
`let' is also the way to create simple functions at the GHCi prompt
<haskell>
Prelude> let fact n = product [1..n]
Prelude> fact 5
120
</haskell>


== GHCi ==


== Checking Types ==
== Checking Types ==

Revision as of 08:11, 31 August 2009

GHC

GHCi

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