コンテンツにスキップ

テンプレート:HaskellExample

出典: フリー教科書『ウィキブックス(Wikibooks)』

Example: {{{1}}}

{{{2}}}
テンプレートの解説

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 <syntaxhighlight lang="haskell">...</syntaxhighlight> 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=<syntaxhighlight lang="haskell">
abs x
    | x < 0     = 0 - x
    | otherwise = x
</syntaxhighlight>}}

Is rendered as:

Example: The abs function.

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

Another example:

{{HaskellExample|1=The signum function.|2=
<syntaxhighlight lang="haskell">
mySignum x =
    if x < 0 then 
        -1
    else if x > 0 then 
        1
    else 
        0
</syntaxhighlight>
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.