## Problem 1

(*) Find the last element of a list.

(Note that the Lisp transcription of this problem is incorrect.)

Solutions

## Problem 2

(*) Find the last but one element of a list.

(Note that the Lisp transcription of this problem is incorrect.)

Solutions

## Problem 3

(*) Find the K’th element of a list. The first element in the list is number 1.

Example:

Solutions

## Problem 4

(*) Find the number of elements of a list.

Solutions

## Problem 5

(*) Reverse a list.

Solutions

foldl相比foldr，采用了tail recursion，所以可以进行更进一步的优化得到foldl'

## Problem 6

(*) Find out whether a list is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x).

Solutions

## Problem 7

(**) Flatten a nested list structure.

Transform a list, possibly holding lists as elements into a `flat’ list by replacing each list with its elements (recursively).

Example:

We have to define a new data type, because lists in Haskell are homogeneous.

Solutions

## Problem 8

(**) Eliminate consecutive duplicates of list elements.

If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.

Example:

Solutions

## Problem 9

(**) Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.

Example:

Solutions

## Problem 10

(*) Run-length encoding of a list. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.

Example:

Solutions

------ 本文结束------
0%