Template:HaskellExample

From Wikibooks, open books for an open world
Jump to: navigation, search

Example: {{{1}}}

{{{2}}}
Template documentation

This template is used for Haskell (source) examples in Haskell books.

If you want do display GHCi transcripts use Template:HaskellGHCi or Template:HaskellGHCiExample.

This template is also used if you want do display Haskell source code examples; in this case use the <source lang="haskell">...</source> tags as shown below (it's not possible to use a custom template for source code because wikimedia syntax can't handle | in template context which is used by Haskell guards).

Source only example:

{{HaskellExample|1=The abs function.|2=<source lang="haskell">
abs x
    | x < 0     = 0 - x
    | otherwise = x
</source>}}

Is rendered as:

Example: The abs function.

abs x
    | x < 0     = 0 - x
    | otherwise = x

Another example:

{{HaskellExample|1=The signum function.|2=
<source lang="haskell">
mySignum x =
    if x < 0 then 
        -1
    else if x > 0 then 
        1
    else 
        0
</source>
You can experiment with this as:

{{HaskellGHCi|1=
*Main> mySignum 5
1
*Main> mySignum 0
0
*Main> mySignum (5-10)
-1
*Main> mySignum (-1)
-1
}}
}}

Is rendered as:

Example: The signum function.

mySignum x =
    if x < 0 then 
        -1
    else if x > 0 then 
        1
    else 
        0

You can experiment with this as:

*Main> mySignum 5
1
*Main> mySignum 0
0
*Main> mySignum (5-10)
-1
*Main> mySignum (-1)
-1

The |1= and |2= are necessary as the text may contain equal signs. Otherwise just {{{1}}} would get displayed.