https://wiki.haskell.org/api.php?action=feedcontributions&user=Mikbe&feedformat=atomHaskellWiki - User contributions [en]2024-03-29T12:45:07ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Sandbox&diff=41745Sandbox2011-08-24T21:39:12Z<p>Mikbe: /* External */</p>
<hr />
<div>''Feel free to edit as you wish on this page. It is here for you to experiment with WIKI edit syntax.''<br />
==section==<br />
[[Sandbox#secton]]<br><br />
<br />
<br />
<br />
--[[User:Lars|Lars]] 11:14, 27 July 2009 (UTC)<br />
----<br />
<math>Insert formula here</math>[[Media:Example.ogg]][[Image:Example.jpg]]<br />
== Headline text ==<br />
''Italic text'''''Bold text'''<br />
{| border="1"<br />
|-<br />
|Pavillon || Uge || Uge <br />
|}<br />
<br />
Matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & 4\end{matrix} \right]</math><br />
<br />
Embedded matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & \left[ \begin{matrix}5 & 6 \\ 7 & 8\end{matrix}\right] \end{matrix} \right]</math><br />
<br />
== Links ==<br />
<br />
An [[internal]] link.<br />
<br />
=== External ===<br />
http://cs.pdx.edu/<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/jpf05.pdf<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/Haskell98.pdf<br />
<br />
Text taken from http://hpaste.org/3881: this wiki's syntax highlight at least does not garble the source, unlike hpaste's highlight.<br />
<br />
An [http://www.haskell.org external link] using text instead of the raw link address.<br />
<br />
<haskell><br />
module Main where<br />
<br />
import Prelude hiding (putStrLn, print)<br />
import System.IO.UTF8<br />
import Data.List (intersperse, find)<br />
<br />
type Персонаж = String<br />
type Персонажи = [Персонаж]<br />
<br />
едоки :: Персонажи<br />
едоки = [ "дедка", "бабка", "внучка", "жучка", "кошка", "мышка" ]<br />
<br />
подходы :: [ Персонажи ]<br />
подходы = scanl позвать [] едоки<br />
where позвать тянущие подмога = подмога:тянущие<br />
<br />
построились :: Персонажи -> [ (Персонаж, Персонаж) ]<br />
построились едоки = zip едоки ("репка":едоки)<br />
<br />
диспозиции = map (построились.reverse) подходы<br />
<br />
описать [] = "Посадил дед репку ..."<br />
описать диспозиция = <br />
unwords ["Взялись:"<br />
, concat $ intersperse ", " $ map за диспозиция<br />
, ". Тянут-потянут -- " ++ result<br />
]<br />
where <br />
за (кто,кого) = кто ++ " за " ++ винительный_падеж кого<br />
винительный_падеж ы = init ы ++ "у"<br />
result = case find ((=="мышка").fst) диспозиция of<br />
Just _ -> "вытянули репку!"<br />
Nothing -> "вытянуть не могут!"<br />
<br />
main = mapM_ putStrLn $ map описать диспозиции</haskell><br />
<br />
== Section ==<br />
=== Subsection ===<br />
==== Subsubsection ====<br />
<br />
huhu<br />
<br />
===== Subsubsubsection =====</div>Mikbehttps://wiki.haskell.org/index.php?title=Common_Misunderstandings&diff=41744Common Misunderstandings2011-08-24T21:37:03Z<p>Mikbe: /* If / Then / Else */</p>
<hr />
<div>[[Category:FAQ]]<br />
<br />
= Common Mistakes and Incorrect Beliefs By Haskell Beginners=<br />
People going from zero to Haskell are likely to gain a misunderstanding or miss a point that isn't stressed enough. Here are some mistakes that have been observed from multiple sources.<br />
<br />
== Indentation ==<br />
Perhaps the first trip-up - you might understand that indentation defines where a code block starts and the lack of an equal amount of indentation indicates the previous code block ended. What some miss is that <hask>then</hask> and <hask>else</hask> must be indented deeper than the <hask>if</hask> statement:<br />
<br />
<haskell><br />
if boolean<br />
then expr1<br />
else expr2<br />
</haskell><br />
<br />
Or they can be on the same line as the if:<br />
<br />
<haskell><br />
if boolean then expr1 else expr2<br />
</haskell><br />
<br />
== If / Then / Else ==<br />
if-then statements must always include an 'else' portion. It might be best not to think of if-then-else as flow control, as in most imperative languages, but think of it as construction of a value using a well formed expression.<br />
<br />
<code><br />
x = b ? y : z;<br />
</code><br />
<br />
The above is valid C code, a [http://en.wikipedia.org/wiki/Ternary_operation ternary operation], that's used quite commonly as a shortcut to typing out a full if-then-else block. It states that if <code>b</code> is true then <code>x = y</code> otherwise <code>x = z</code>. Notice how this makes no sense without <code>z</code>. Similarly, in Haskell an <hask>if</hask>/<hask>then</hask> makes no sense without an <hask>else</hask>.<br />
<br />
<haskell><br />
let x = if b then y -- compare to x = b ? y<br />
</haskell><br />
<br />
What is <code>x</code> when <code>b</code> is false? One should also recognize that the types returned by the <hask>then</hask> and <hask>else</hask> branches must match due to Haskells strong and static type system.<br />
<br />
When <hask>if</hask> is used for sequencing IO it is not uncommon to see an <hask>else</hask> that returns a null value:<br />
<br />
<haskell><br />
main = do<br />
startNetwork <- askUser "Network? "<br />
if startNetwork<br />
then do iface <- initNetworkInterface<br />
handlePackets iface<br />
else return ()<br />
</haskell><br />
<br />
Such uses can be more succinct if they use the <hask>when</hask> function (from the Control.Monad module):<br />
<br />
<haskell><br />
main = do<br />
startNetwork <- askUser "Network? "<br />
when startNetwork $ do<br />
iface <- initNetworkInterface<br />
handlePackets iface<br />
</haskell><br />
<br />
== Binding of Type Signatures ==<br />
Due to the typeclass overloading of numbers, lists of numbers are often typed as:<br />
<br />
<haskell><br />
ghci> [1,2,3,4] :: [Float]<br />
</haskell><br />
<br />
This works fine when the list stands alone, but when applying a function take note the function application binds stronger than the type signature. This means the below signature is invalid:<br />
<br />
<haskell><br />
ghci> map floor [1,2,3,4] :: [Float]<br />
</haskell><br />
<br />
and should instead be:<br />
<haskell><br />
ghci> map floor ([1,2,3,4] :: [Float])<br />
</haskell><br />
<br />
== '''do''' Notation ==<br />
If the [[do notation]] page ever exists I'll advise you to check it out. Until then, understand that a missing <hask>do</hask> from the top of a function or code block can result in your compiler giving an error message citing a much later line number. Also, any new blocks (ex: from an <hask>if</hask> or <hask>case</hask>) must have their own <hask>do</hask>, even if the higher level code block already had one.<br />
<br />
Sorry this isn't the full picture - for an inverse point of view see [[do notation considered harmful]].<br />
<br />
== Iterating Over a List ==<br />
Some beginners make the mistake of mistaking a single-element list pattern (such as <hask>[x]</hask>) for a pattern that iterates over every element in the list.<br />
<br />
One example that recently (in April, 2008) appeared on the Haskell-Cafe mailing list (see the reply post [http://www.haskell.org/pipermail/haskell-cafe/2008-April/041701.html Re: Embedding newlines into a string?]) was the following. Here, one coder attempted to write a function <hask>hanoi</hask> to solve the Towers of Hanoi problem, but to code it so that each tower could be named polymorphically, using, for example, either Chars or Ints. The problematic code segment was the following:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [(a, b)] = "Move " ++ show a ++ " to " ++ show b ++ "."<br />
</haskell><br />
<br />
in the following program:<br />
<br />
<haskell><br />
hanoi :: a -> a -> a -> Int -> [(a, a)]<br />
hanoi source using dest n<br />
| n == 1 = [(source, dest)]<br />
| otherwise = hanoi source dest using (n-1)<br />
++ hanoi source using dest 1<br />
++ hanoi using source dest (n-1)<br />
<br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [(a, b)] = "Move " ++ show a ++ " to " ++ show b ++ "."<br />
</haskell><br />
<br />
The coder tried to run the code in WinHugs as follows:<br />
<br />
<hask><br />
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))<br />
</hask><br />
<br />
However, this was the result:<br />
<br />
<hask><br />
Program error: pattern match failure: hanoi_shower<br />
[('a','b'),('a','c')] ++ ([] ++ hanoi 'b' 'a' 'c' (2 - 1))<br />
</hask><br />
<br />
The problem was that the parameter <hask>[(a, b)]</hask> to <hask>hanoi_shower</hask> only matched the first element of the list, but didn't iterate over the list as intended.<br />
<br />
Here is a corrected version of the code above:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower moves = unlines ["Move " ++ show a ++ " to "++ show b ++ "." | (a, b) <- moves]<br />
</haskell><br />
<br />
Here, <hask>moves</hask> is pattern-matched to type <hask>[(a, a)]</hask> (a list of pairs). The problem is how to iterate over the elements (pairs) of the list while separating the first <hask>a</hask> of each pair from the second <hask>a</hask>.<br />
<br />
The solution above uses list comprehension: The generator <hask>(a, b) <- moves</hask> feeds each pair in turn to the left-hand expression <hask>(a, b)</hask>, and this pair is mapped to the left expression, <hask>"Move " ++ show a ++ " to "++ show b ++ "."</hask>, building a new list of sentences representing moves. Then, the function <hask>unlines</hask> breaks this list into a sequence of lines.<br />
<br />
Here is the result of executing the above code in WinHugs:<br />
<br />
<hask><br />
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))<br />
Move 'a' to 'b'.<br />
Move 'a' to 'c'.<br />
Move 'b' to 'c'.<br />
<br />
Main> putStr (hanoi_shower (hanoi 1 2 3 2))<br />
Move 1 to 2.<br />
Move 1 to 3.<br />
Move 2 to 3.<br />
</hask><br />
<br />
Notice that since <hask>a</hask> and <hask>b</hask> in <hask>(a, b)</hask> are polymorphic types, they can range over both <hask>Chars</hask> and <hask>Ints</hask>.<br />
<br />
Another way of writing <hask>hanoi_shower</hask>, using <hask>map</hask>, is as follows:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower moves = unlines (map move moves)<br />
where move (a, b) = "Move " ++ show a ++ " to "++ show b ++ "."<br />
</haskell><br />
<br />
Here, <hask>move</hask> is mapped over <hask>moves</hask>, and each pair <hask>(a, b)</hask> of <hask>moves</hask> is pattern-matched against <hask>"Move " ++ show a ++ " to "++ show b ++ "."</hask><br />
<br />
Another way to map over a list is to use recursion, although this method is considered non-idiomatic Haskell (Haskellers generally prefer using higher-order functions over recursion when possible):<br />
<br />
<haskell><br />
hanoi :: a -> a -> a -> Int -> [(a, a)]<br />
hanoi source using dest n<br />
| n == 0 = []<br />
| n == 1 = [(source, dest)]<br />
| otherwise = hanoi source dest using (n-1) <br />
++ hanoi source using dest 1<br />
++ hanoi using source dest (n-1)<br />
<br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [] = ""<br />
hanoi_shower ((a, b):moves) = unlines ["Move " ++ show a ++ " to "++ show b ++ "."] ++ hanoi_shower moves<br />
</haskell><br />
<br />
Here, in <hask>hanoi_shower</hask>, the base case is simply an empty list <hask>[]</hask>. At each recursive step, a list of type <hask>[(a, a)]</hask> (a list of pairs) is mapped against the parameter <hask>(a, b):moves</hask> of <hask>hanoi_shower</hask>. This separates the head of the list <hask>(a, b)</hask> from the tail of the list <hask>moves</hask>, which then is further matched against <hask>((a, b):moves)</hask> on the next recursive call.<br />
<br />
There are other ways of iterating over lists as well. One advantage of Haskell is that there are often many ways of performing the same action, including iterating over lists.<br />
<br />
== Guards ==<br />
(The following two tips on guards were contributed by the user 7stud in the thread "Top beginner mistakes" (see http://article.gmane.org/gmane.comp.lang.haskell.beginners/1121) on the Haskell-Beginners mailing list on Wed, 4 Mar 2009 21:54:05 +0000 (UTC).)<br />
<br />
Some beginners make the mistake of putting an equals sign after a function name when using guards; ''viz.'':<br />
<br />
<haskell><br />
myfunction x y =<br />
| x < 2 = "a"<br />
| y > 20 = "b"<br />
| otherwise = "c"<br />
</haskell><br />
<br />
This causes a cryptic error message similar to the following to be displayed:<br />
<br />
<hask><br />
dhask.hs:2:4: parse error on input `|'<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
Another common mistake that some beginners make is writing "if" in front of the guard conditions; ''viz.'':<br />
<br />
<haskell><br />
myfunction x y<br />
| if x < 2 = "a"<br />
| if y > 20 = "b"<br />
| otherwise = "c"<br />
</haskell><br />
<br />
This causes a mysterious error message similar to the following to be shown:<br />
<br />
<hask><br />
dhask.hs:2:25: parse error on input `='<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
In both cases, the error messages don't help to identify the problem.<br />
<br />
(Entry added by [[User:DekuDekuplex|Benjamin L. Russell]].)<br />
<br />
== Parentheses ==<br />
(The following tip on parentheses was contributed by the user 7stud in the thread "Top beginner mistakes" (see http://article.gmane.org/gmane.comp.lang.haskell.beginners/1121) on the Haskell-Beginners mailing list on Wed, 4 Mar 2009 21:54:05 +0000 (UTC).)<br />
<br />
Some beginners make the mistake of not putting parentheses around arguments of the form x:xs; ''viz.'':<br />
<br />
<haskell><br />
dosomething x:xs = head xs<br />
</haskell><br />
<br />
This causes an ambiguous error message similar to the following to be shown:<br />
<br />
<hask><br />
dhask.hs:1:0: Parse error in pattern<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
Here, the error message doesn't help to recognize the problem.<br />
<br />
(Entry added by [[User:DekuDekuplex|Benjamin L. Russell]].)</div>Mikbehttps://wiki.haskell.org/index.php?title=Common_Misunderstandings&diff=41743Common Misunderstandings2011-08-24T21:36:08Z<p>Mikbe: /* If / Then / Else */</p>
<hr />
<div>[[Category:FAQ]]<br />
<br />
= Common Mistakes and Incorrect Beliefs By Haskell Beginners=<br />
People going from zero to Haskell are likely to gain a misunderstanding or miss a point that isn't stressed enough. Here are some mistakes that have been observed from multiple sources.<br />
<br />
== Indentation ==<br />
Perhaps the first trip-up - you might understand that indentation defines where a code block starts and the lack of an equal amount of indentation indicates the previous code block ended. What some miss is that <hask>then</hask> and <hask>else</hask> must be indented deeper than the <hask>if</hask> statement:<br />
<br />
<haskell><br />
if boolean<br />
then expr1<br />
else expr2<br />
</haskell><br />
<br />
Or they can be on the same line as the if:<br />
<br />
<haskell><br />
if boolean then expr1 else expr2<br />
</haskell><br />
<br />
== If / Then / Else ==<br />
if-then statements must always include an 'else' portion. It might be best not to think of if-then-else as flow control, as in most imperative languages, but think of it as construction of a value using a well formed expression.<br />
<br />
<code><br />
x = b ? y : z;<br />
</code><br />
<br />
The above is valid C code, a [[ternary operation](http://en.wikipedia.org/wiki/Ternary_operation)], that's used quite commonly as a shortcut to typing out a full if-then-else block. It states that if <code>b</code> is true then <code>x = y</code> otherwise <code>x = z</code>. Notice how this makes no sense without <code>z</code>. Similarly, in Haskell an <hask>if</hask>/<hask>then</hask> makes no sense without an <hask>else</hask>.<br />
<br />
<haskell><br />
let x = if b then y -- compare to x = b ? y<br />
</haskell><br />
<br />
What is <code>x</code> when <code>b</code> is false? One should also recognize that the types returned by the <hask>then</hask> and <hask>else</hask> branches must match due to Haskells strong and static type system.<br />
<br />
When <hask>if</hask> is used for sequencing IO it is not uncommon to see an <hask>else</hask> that returns a null value:<br />
<br />
<haskell><br />
main = do<br />
startNetwork <- askUser "Network? "<br />
if startNetwork<br />
then do iface <- initNetworkInterface<br />
handlePackets iface<br />
else return ()<br />
</haskell><br />
<br />
Such uses can be more succinct if they use the <hask>when</hask> function (from the Control.Monad module):<br />
<br />
<haskell><br />
main = do<br />
startNetwork <- askUser "Network? "<br />
when startNetwork $ do<br />
iface <- initNetworkInterface<br />
handlePackets iface<br />
</haskell><br />
<br />
== Binding of Type Signatures ==<br />
Due to the typeclass overloading of numbers, lists of numbers are often typed as:<br />
<br />
<haskell><br />
ghci> [1,2,3,4] :: [Float]<br />
</haskell><br />
<br />
This works fine when the list stands alone, but when applying a function take note the function application binds stronger than the type signature. This means the below signature is invalid:<br />
<br />
<haskell><br />
ghci> map floor [1,2,3,4] :: [Float]<br />
</haskell><br />
<br />
and should instead be:<br />
<haskell><br />
ghci> map floor ([1,2,3,4] :: [Float])<br />
</haskell><br />
<br />
== '''do''' Notation ==<br />
If the [[do notation]] page ever exists I'll advise you to check it out. Until then, understand that a missing <hask>do</hask> from the top of a function or code block can result in your compiler giving an error message citing a much later line number. Also, any new blocks (ex: from an <hask>if</hask> or <hask>case</hask>) must have their own <hask>do</hask>, even if the higher level code block already had one.<br />
<br />
Sorry this isn't the full picture - for an inverse point of view see [[do notation considered harmful]].<br />
<br />
== Iterating Over a List ==<br />
Some beginners make the mistake of mistaking a single-element list pattern (such as <hask>[x]</hask>) for a pattern that iterates over every element in the list.<br />
<br />
One example that recently (in April, 2008) appeared on the Haskell-Cafe mailing list (see the reply post [http://www.haskell.org/pipermail/haskell-cafe/2008-April/041701.html Re: Embedding newlines into a string?]) was the following. Here, one coder attempted to write a function <hask>hanoi</hask> to solve the Towers of Hanoi problem, but to code it so that each tower could be named polymorphically, using, for example, either Chars or Ints. The problematic code segment was the following:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [(a, b)] = "Move " ++ show a ++ " to " ++ show b ++ "."<br />
</haskell><br />
<br />
in the following program:<br />
<br />
<haskell><br />
hanoi :: a -> a -> a -> Int -> [(a, a)]<br />
hanoi source using dest n<br />
| n == 1 = [(source, dest)]<br />
| otherwise = hanoi source dest using (n-1)<br />
++ hanoi source using dest 1<br />
++ hanoi using source dest (n-1)<br />
<br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [(a, b)] = "Move " ++ show a ++ " to " ++ show b ++ "."<br />
</haskell><br />
<br />
The coder tried to run the code in WinHugs as follows:<br />
<br />
<hask><br />
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))<br />
</hask><br />
<br />
However, this was the result:<br />
<br />
<hask><br />
Program error: pattern match failure: hanoi_shower<br />
[('a','b'),('a','c')] ++ ([] ++ hanoi 'b' 'a' 'c' (2 - 1))<br />
</hask><br />
<br />
The problem was that the parameter <hask>[(a, b)]</hask> to <hask>hanoi_shower</hask> only matched the first element of the list, but didn't iterate over the list as intended.<br />
<br />
Here is a corrected version of the code above:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower moves = unlines ["Move " ++ show a ++ " to "++ show b ++ "." | (a, b) <- moves]<br />
</haskell><br />
<br />
Here, <hask>moves</hask> is pattern-matched to type <hask>[(a, a)]</hask> (a list of pairs). The problem is how to iterate over the elements (pairs) of the list while separating the first <hask>a</hask> of each pair from the second <hask>a</hask>.<br />
<br />
The solution above uses list comprehension: The generator <hask>(a, b) <- moves</hask> feeds each pair in turn to the left-hand expression <hask>(a, b)</hask>, and this pair is mapped to the left expression, <hask>"Move " ++ show a ++ " to "++ show b ++ "."</hask>, building a new list of sentences representing moves. Then, the function <hask>unlines</hask> breaks this list into a sequence of lines.<br />
<br />
Here is the result of executing the above code in WinHugs:<br />
<br />
<hask><br />
Main> putStr (hanoi_shower (hanoi 'a' 'b' 'c' 2))<br />
Move 'a' to 'b'.<br />
Move 'a' to 'c'.<br />
Move 'b' to 'c'.<br />
<br />
Main> putStr (hanoi_shower (hanoi 1 2 3 2))<br />
Move 1 to 2.<br />
Move 1 to 3.<br />
Move 2 to 3.<br />
</hask><br />
<br />
Notice that since <hask>a</hask> and <hask>b</hask> in <hask>(a, b)</hask> are polymorphic types, they can range over both <hask>Chars</hask> and <hask>Ints</hask>.<br />
<br />
Another way of writing <hask>hanoi_shower</hask>, using <hask>map</hask>, is as follows:<br />
<br />
<haskell><br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower moves = unlines (map move moves)<br />
where move (a, b) = "Move " ++ show a ++ " to "++ show b ++ "."<br />
</haskell><br />
<br />
Here, <hask>move</hask> is mapped over <hask>moves</hask>, and each pair <hask>(a, b)</hask> of <hask>moves</hask> is pattern-matched against <hask>"Move " ++ show a ++ " to "++ show b ++ "."</hask><br />
<br />
Another way to map over a list is to use recursion, although this method is considered non-idiomatic Haskell (Haskellers generally prefer using higher-order functions over recursion when possible):<br />
<br />
<haskell><br />
hanoi :: a -> a -> a -> Int -> [(a, a)]<br />
hanoi source using dest n<br />
| n == 0 = []<br />
| n == 1 = [(source, dest)]<br />
| otherwise = hanoi source dest using (n-1) <br />
++ hanoi source using dest 1<br />
++ hanoi using source dest (n-1)<br />
<br />
hanoi_shower :: Show a => [(a, a)] -> String<br />
hanoi_shower [] = ""<br />
hanoi_shower ((a, b):moves) = unlines ["Move " ++ show a ++ " to "++ show b ++ "."] ++ hanoi_shower moves<br />
</haskell><br />
<br />
Here, in <hask>hanoi_shower</hask>, the base case is simply an empty list <hask>[]</hask>. At each recursive step, a list of type <hask>[(a, a)]</hask> (a list of pairs) is mapped against the parameter <hask>(a, b):moves</hask> of <hask>hanoi_shower</hask>. This separates the head of the list <hask>(a, b)</hask> from the tail of the list <hask>moves</hask>, which then is further matched against <hask>((a, b):moves)</hask> on the next recursive call.<br />
<br />
There are other ways of iterating over lists as well. One advantage of Haskell is that there are often many ways of performing the same action, including iterating over lists.<br />
<br />
== Guards ==<br />
(The following two tips on guards were contributed by the user 7stud in the thread "Top beginner mistakes" (see http://article.gmane.org/gmane.comp.lang.haskell.beginners/1121) on the Haskell-Beginners mailing list on Wed, 4 Mar 2009 21:54:05 +0000 (UTC).)<br />
<br />
Some beginners make the mistake of putting an equals sign after a function name when using guards; ''viz.'':<br />
<br />
<haskell><br />
myfunction x y =<br />
| x < 2 = "a"<br />
| y > 20 = "b"<br />
| otherwise = "c"<br />
</haskell><br />
<br />
This causes a cryptic error message similar to the following to be displayed:<br />
<br />
<hask><br />
dhask.hs:2:4: parse error on input `|'<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
Another common mistake that some beginners make is writing "if" in front of the guard conditions; ''viz.'':<br />
<br />
<haskell><br />
myfunction x y<br />
| if x < 2 = "a"<br />
| if y > 20 = "b"<br />
| otherwise = "c"<br />
</haskell><br />
<br />
This causes a mysterious error message similar to the following to be shown:<br />
<br />
<hask><br />
dhask.hs:2:25: parse error on input `='<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
In both cases, the error messages don't help to identify the problem.<br />
<br />
(Entry added by [[User:DekuDekuplex|Benjamin L. Russell]].)<br />
<br />
== Parentheses ==<br />
(The following tip on parentheses was contributed by the user 7stud in the thread "Top beginner mistakes" (see http://article.gmane.org/gmane.comp.lang.haskell.beginners/1121) on the Haskell-Beginners mailing list on Wed, 4 Mar 2009 21:54:05 +0000 (UTC).)<br />
<br />
Some beginners make the mistake of not putting parentheses around arguments of the form x:xs; ''viz.'':<br />
<br />
<haskell><br />
dosomething x:xs = head xs<br />
</haskell><br />
<br />
This causes an ambiguous error message similar to the following to be shown:<br />
<br />
<hask><br />
dhask.hs:1:0: Parse error in pattern<br />
Failed, modules loaded: none.<br />
</hask><br />
<br />
Here, the error message doesn't help to recognize the problem.<br />
<br />
(Entry added by [[User:DekuDekuplex|Benjamin L. Russell]].)</div>Mikbehttps://wiki.haskell.org/index.php?title=Sandbox&diff=41742Sandbox2011-08-24T21:34:29Z<p>Mikbe: /* Links */</p>
<hr />
<div>''Feel free to edit as you wish on this page. It is here for you to experiment with WIKI edit syntax.''<br />
==section==<br />
[[Sandbox#secton]]<br><br />
<br />
<br />
<br />
--[[User:Lars|Lars]] 11:14, 27 July 2009 (UTC)<br />
----<br />
<math>Insert formula here</math>[[Media:Example.ogg]][[Image:Example.jpg]]<br />
== Headline text ==<br />
''Italic text'''''Bold text'''<br />
{| border="1"<br />
|-<br />
|Pavillon || Uge || Uge <br />
|}<br />
<br />
Matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & 4\end{matrix} \right]</math><br />
<br />
Embedded matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & \left[ \begin{matrix}5 & 6 \\ 7 & 8\end{matrix}\right] \end{matrix} \right]</math><br />
<br />
== Links ==<br />
<br />
An [[internal]] link.<br />
<br />
=== External ===<br />
http://cs.pdx.edu/<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/jpf05.pdf<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/Haskell98.pdf<br />
<br />
Text taken from http://hpaste.org/3881: this wiki's syntax highlight at least does not garble the source, unlike hpaste's highlight.<br />
<br />
<haskell><br />
module Main where<br />
<br />
import Prelude hiding (putStrLn, print)<br />
import System.IO.UTF8<br />
import Data.List (intersperse, find)<br />
<br />
type Персонаж = String<br />
type Персонажи = [Персонаж]<br />
<br />
едоки :: Персонажи<br />
едоки = [ "дедка", "бабка", "внучка", "жучка", "кошка", "мышка" ]<br />
<br />
подходы :: [ Персонажи ]<br />
подходы = scanl позвать [] едоки<br />
where позвать тянущие подмога = подмога:тянущие<br />
<br />
построились :: Персонажи -> [ (Персонаж, Персонаж) ]<br />
построились едоки = zip едоки ("репка":едоки)<br />
<br />
диспозиции = map (построились.reverse) подходы<br />
<br />
описать [] = "Посадил дед репку ..."<br />
описать диспозиция = <br />
unwords ["Взялись:"<br />
, concat $ intersperse ", " $ map за диспозиция<br />
, ". Тянут-потянут -- " ++ result<br />
]<br />
where <br />
за (кто,кого) = кто ++ " за " ++ винительный_падеж кого<br />
винительный_падеж ы = init ы ++ "у"<br />
result = case find ((=="мышка").fst) диспозиция of<br />
Just _ -> "вытянули репку!"<br />
Nothing -> "вытянуть не могут!"<br />
<br />
main = mapM_ putStrLn $ map описать диспозиции</haskell><br />
<br />
== Section ==<br />
=== Subsection ===<br />
==== Subsubsection ====<br />
<br />
huhu<br />
<br />
===== Subsubsubsection =====</div>Mikbehttps://wiki.haskell.org/index.php?title=Sandbox&diff=41741Sandbox2011-08-24T21:27:58Z<p>Mikbe: /* Headline text */</p>
<hr />
<div>''Feel free to edit as you wish on this page. It is here for you to experiment with WIKI edit syntax.''<br />
==section==<br />
[[Sandbox#secton]]<br><br />
<br />
<br />
<br />
--[[User:Lars|Lars]] 11:14, 27 July 2009 (UTC)<br />
----<br />
<math>Insert formula here</math>[[Media:Example.ogg]][[Image:Example.jpg]]<br />
== Headline text ==<br />
''Italic text'''''Bold text'''<br />
{| border="1"<br />
|-<br />
|Pavillon || Uge || Uge <br />
|}<br />
<br />
Matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & 4\end{matrix} \right]</math><br />
<br />
Embedded matrix:<br />
:<math>\left[ \begin{matrix}1 & 2 \\ 3 & \left[ \begin{matrix}5 & 6 \\ 7 & 8\end{matrix}\right] \end{matrix} \right]</math><br />
<br />
== Links ==<br />
<br />
An [internal] link.<br />
<br />
=== External ===<br />
http://cs.pdx.edu/<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/jpf05.pdf<br />
:<br />
http://cs.pdx.edu/~dick/HaskellSemantics/Haskell98.pdf<br />
<br />
Text taken from http://hpaste.org/3881: this wiki's syntax highlight at least does not garble the source, unlike hpaste's highlight.<br />
<br />
<haskell><br />
module Main where<br />
<br />
import Prelude hiding (putStrLn, print)<br />
import System.IO.UTF8<br />
import Data.List (intersperse, find)<br />
<br />
type Персонаж = String<br />
type Персонажи = [Персонаж]<br />
<br />
едоки :: Персонажи<br />
едоки = [ "дедка", "бабка", "внучка", "жучка", "кошка", "мышка" ]<br />
<br />
подходы :: [ Персонажи ]<br />
подходы = scanl позвать [] едоки<br />
where позвать тянущие подмога = подмога:тянущие<br />
<br />
построились :: Персонажи -> [ (Персонаж, Персонаж) ]<br />
построились едоки = zip едоки ("репка":едоки)<br />
<br />
диспозиции = map (построились.reverse) подходы<br />
<br />
описать [] = "Посадил дед репку ..."<br />
описать диспозиция = <br />
unwords ["Взялись:"<br />
, concat $ intersperse ", " $ map за диспозиция<br />
, ". Тянут-потянут -- " ++ result<br />
]<br />
where <br />
за (кто,кого) = кто ++ " за " ++ винительный_падеж кого<br />
винительный_падеж ы = init ы ++ "у"<br />
result = case find ((=="мышка").fst) диспозиция of<br />
Just _ -> "вытянули репку!"<br />
Nothing -> "вытянуть не могут!"<br />
<br />
main = mapM_ putStrLn $ map описать диспозиции</haskell><br />
<br />
== Section ==<br />
=== Subsection ===<br />
==== Subsubsection ====<br />
<br />
huhu<br />
<br />
===== Subsubsubsection =====</div>Mikbehttps://wiki.haskell.org/index.php?title=Haskell_Quiz/FizzBuzz/Solution_Mikbe&diff=41725Haskell Quiz/FizzBuzz/Solution Mikbe2011-08-23T08:28:31Z<p>Mikbe: </p>
<hr />
<div>I just started learning Haskell literally this week so I went with simple and straight forward. Can't wait till I learn a little so I can make this a one liner.<br />
<br />
<haskell><br />
module Main where<br />
<br />
main :: IO ()<br />
main = do <br />
mapM_ (putStrLn) [fizzBuzz x | x <- [1..100]]<br />
<br />
fizz::Int->String<br />
fizz x = if x `mod` 3 == 0 then "Fizz" else ""<br />
<br />
buzz::Int->String<br />
buzz x = if x `mod` 5 == 0 then "Buzz" else ""<br />
<br />
fizzBuzz::Int->String<br />
fizzBuzz x = if fizz(x) ++ buzz(x) == "" <br />
then show x <br />
else fizz(x) ++ buzz(x)<br />
</haskell></div>Mikbehttps://wiki.haskell.org/index.php?title=Haskell_Quiz/FizzBuzz/Solution_Mikbe&diff=41724Haskell Quiz/FizzBuzz/Solution Mikbe2011-08-23T08:27:00Z<p>Mikbe: New page: I just started learning Haskell literally this week so I went with simple and straight forward. Can't wait till I learn a little so I can make this a one liner. <haskell> module Main wher...</p>
<hr />
<div>I just started learning Haskell literally this week so I went with simple and straight forward. Can't wait till I learn a little so I can make this a one liner.<br />
<br />
<haskell><br />
module Main where<br />
<br />
main :: IO ()<br />
main = do <br />
mapM_ (putStrLn . show) [fizzBuzz x | x <- [1..100]]<br />
<br />
fizz::Int->String<br />
fizz x = if x `mod` 3 == 0 then "Fizz" else ""<br />
<br />
buzz::Int->String<br />
buzz x = if x `mod` 5 == 0 then "Buzz" else ""<br />
<br />
fizzBuzz::Int->String<br />
fizzBuzz x = if fizz(x) ++ buzz(x) == "" <br />
then show x <br />
else fizz(x) ++ buzz(x)<br />
</haskell></div>Mikbehttps://wiki.haskell.org/index.php?title=Haskell_Quiz/FizzBuzz&diff=41723Haskell Quiz/FizzBuzz2011-08-23T07:49:39Z<p>Mikbe: /* Solutions */</p>
<hr />
<div>"There has been some debate on the proper ways to screen programmers you intend to hire. A common theory is that you really need to have the programmer write some code for you to accurately gauge their skill. Exactly what to have them write is another debate, but the blogosphere has recently been abuzz with this question as a screener:<br />
<br />
Write a program that prints the numbers from 1 to 100.<br />
But for multiples of three print “Fizz” instead of the<br />
number and for the multiples of five print “Buzz”. For<br />
numbers which are multiples of both three and five<br />
print “FizzBuzz”.<br />
<br />
Pretend you've just walked into a job interview and been hit with this question. Solve it as you would under such circumstances for this week's Ruby Quiz."<br />
<br />
==The problem==<br />
<br />
* http://www.rubyquiz.com/quiz126.html<br />
<br />
==Solutions==<br />
<br />
* [[Haskell Quiz/FizzBuzz/Solution Ninju|Alex Watt]]<br />
* [[Haskell Quiz/FizzBuzz/Solution Syzygies|Dave Bayer]]<br />
* [[Haskell Quiz/FizzBuzz/Solution Mikbe|Mike Bethany]]<br />
<br />
[[Category:Haskell Quiz|FizzBuzz]]</div>Mikbe