Difference between revisions of "99 questions/11 to 20"

From HaskellWiki
Jump to navigation Jump to search
Line 160: Line 160:
 
== Problem 19 ==
 
== Problem 19 ==
   
  +
(**) Rotate a list N places to the left.
<Problem description>
 
  +
shoffsta is working on this...
 
  +
Hint: Use the predefined functions length and (++).
   
 
<pre>
 
<pre>
  +
Examples:
Example:
 
  +
* (rotate '(a b c d e f g h) 3)
<example in lisp>
 
 
(D E F G H A B C)
   
  +
* (rotate '(a b c d e f g h) -2)
Example in Haskell:
 
  +
(G H A B C D E F)
<example in Haskell>
 
  +
 
Examples in Haskell:
  +
*Main> rotate ['a','b','c','d','e','f','g','h'] 3
  +
  +
*Main> rotate ['a','b','c','d','e','f','g','h'] (-2)
 
</pre>
 
</pre>
   
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
  +
rotate [] _ = []
<solution in haskell>
 
  +
rotate l 0 = l
  +
rotate (x:xs) (n+1) = rotate (xs ++ [x]) n
  +
rotate l n = rotate l (length l + n)
 
</haskell>
 
</haskell>
   
  +
There are two separate cases:
<description of implementation>
 
  +
<br/>- If n > 0, move the first element of the list to the end of the list n times.
 
  +
<br/>- If n < 0, convert the problem to the equivalent problem for n > 0 by adding the list's length to n.
  +
  +
 
== Problem 20 ==
 
== Problem 20 ==
   

Revision as of 07:23, 12 December 2006


These are Haskell translations of Ninety Nine Lisp Problems.

If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields.


Problem 11

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 12

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 13

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 14

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 15

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 16

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 17

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 18

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 19

(**) Rotate a list N places to the left.

Hint: Use the predefined functions length and (++).

Examples:
* (rotate '(a b c d e f g h) 3)
(D E F G H A B C)

* (rotate '(a b c d e f g h) -2)
(G H A B C D E F)

Examples in Haskell:
*Main> rotate ['a','b','c','d','e','f','g','h'] 3

*Main> rotate ['a','b','c','d','e','f','g','h'] (-2)

Solution:

rotate [] _ = []
rotate l 0 = l
rotate (x:xs) (n+1) = rotate (xs ++ [x]) n
rotate l n = rotate l (length l + n)

There are two separate cases:
- If n > 0, move the first element of the list to the end of the list n times.
- If n < 0, convert the problem to the equivalent problem for n > 0 by adding the list's length to n.


Problem 20

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>