Cookbook/Compilers and interpreters: Difference between revisions

From HaskellWiki
No edit summary
 
(→‎General: use a less intimidating example)
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Prelude =
== GHC ==


A lot of functions are defined in the "[http://www.haskell.org/hoogle/?q=Prelude 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.
{| class="wikitable"
|-
!  Problem
!  Solution
!  Examples
|-
|  compiling and linking an executable
|  --make
|<pre>ghc --make Main.hs              --> Main</pre>
|-
|  compiling without linking
|  -c
|<pre>ghc -c Foo.hs                    --> Foo.hi, Foo.o</pre>
|-
|  generating Assembler code
|  -S
|<pre>ghc -S Foo.hs                    --> Foo.hi, Foo.s</pre>
|-
|  generating C code
|  -C
|<pre>ghc -C Foo.hs                    --> Foo.hc, Foo.hi</pre>
|-
|  linking files into an executable
|  -o
|<pre>ghc -o test Foo.o Bar.o Baz.p    --> test</pre>
|}


= GHCi/Hugs =
== GHCi ==
== GHCi interaction ==
To start GHCi from a command prompt, simply type `ghci'


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


[http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Prelude] is the "base" library of Haskell.
{| class="wikitable"
|-
!  Problem
!  Solution
!  Examples
|-
|  checking a definition
:i
|<haskell>
Prelude> :i True
data Bool = ... | True  -- Defined in GHC.Bool
</haskell>
|-
|  checking a type
|  :t
|<haskell>
Prelude> :t "Hello"
"Hello" :: [Char]


To create variables at the GHCi prompt, use `let'
Prelude> :t length
<haskell>
length :: [a] -> Int
Prelude> let x = 5
</haskell>
Prelude> x
|-
5
|  loading a file
Prelude> let y = 3
|  :l
Prelude> y
|<haskell>
3
Prelude> :l Foo.hs
Prelude> x + y
[1 of 1] Compiling Foo              ( Foo.hs, interpreted )
8
Ok, modules loaded: Foo.
</haskell>
|-
|  reloading all loaded files
|  :r
|<haskell>
Prelude> :r
</haskell>
</haskell>
|}
=== Debugging ===


`let' is also the way to create simple functions at the GHCi prompt
{| class="wikitable"
<haskell>
|-
Prelude> let fact n = product [1..n]
!  Problem
Prelude> fact 5
!  Solution
120
!  Examples
|-
|  setting a break point
|  :break
|<haskell>
Prelude> :break 2    -- sets a break point in line 2
</haskell>
</haskell>
|}


== Hugs ==


== Checking Types ==
TODO
To check the type of an expression or function, use the command `:t'
<haskell>
Prelude> :t x
x :: Integer
Prelude> :t "Hello"
"Hello" :: [Char]
</haskell>
Haskell has the following types defined in the [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Standard Prelude].
<haskell>
    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
</haskell>

Latest revision as of 08:33, 25 January 2010

GHC

Problem Solution Examples
compiling and linking an executable --make
ghc --make Main.hs               --> Main
compiling without linking -c
ghc -c Foo.hs                    --> Foo.hi, Foo.o
generating Assembler code -S
ghc -S Foo.hs                    --> Foo.hi, Foo.s
generating C code -C
ghc -C Foo.hs                    --> Foo.hc, Foo.hi
linking files into an executable -o
ghc -o test Foo.o Bar.o Baz.p    --> test

GHCi

General

Problem Solution Examples
checking a definition :i
Prelude> :i True
data Bool = ... | True  -- Defined in GHC.Bool
checking a type :t
Prelude> :t "Hello"
"Hello" :: [Char]

Prelude> :t length
length :: [a] -> Int
loading a file :l
Prelude> :l Foo.hs
[1 of 1] Compiling Foo              ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
reloading all loaded files :r
Prelude> :r

Debugging

Problem Solution Examples
setting a break point :break
Prelude> :break 2    -- sets a break point in line 2

Hugs

TODO