# Difference between revisions of "H-99: Ninety-Nine Haskell Problems"

From HaskellWiki

(added Category:Tutorials and used == heading instead of ===) |
(added chessguy from #haskell's solution to problem 2) |
||

Line 16: | Line 16: | ||

last [x] = x |
last [x] = x |
||

last (_:xs) = last xs |
last (_:xs) = last xs |
||

+ | </haskell> |
||

+ | |||

+ | == Problem 2 == |
||

+ | |||

+ | <pre> |
||

+ | (*) Find the last but one box of a list. |
||

+ | Example: |
||

+ | * (my-but-last '(a b c d)) |
||

+ | (C D) |
||

+ | </pre> |
||

+ | |||

+ | This can be done by dropping all but the last two elements of a list: |
||

+ | |||

+ | <haskell> |
||

+ | myButLast :: [a] -> [a] |
||

+ | myButLast list = drop ((length list) - 2) list |
||

</haskell> |
</haskell> |
||

## Revision as of 04:22, 12 December 2006

These are Haskell translations of Ninety Nine Lisp Problems.

## Problem 1

(*) Find the last box of a list. Example: * (my-last '(a b c d)) (D)

This is "last" in Prelude, which is defined as:

```
last :: [a] -> a
last [x] = x
last (_:xs) = last xs
```

## Problem 2

(*) Find the last but one box of a list. Example: * (my-but-last '(a b c d)) (C D)

This can be done by dropping all but the last two elements of a list:

```
myButLast :: [a] -> [a]
myButLast list = drop ((length list) - 2) list
```