99 scala problems - part 1

by Gregor Uhlenheuer on August 9, 2012

In order to get some more practice on programming in scala I started solving some problems of the probably well known 99 problems of other functional programming languages. I first found that kind of series on the haskell wiki that mentions the origin being the Ninety-Nine Prolog Problems.

This post now contains my solutions to the first 10 problems mentioned on the haskell wiki translated to scala. This first problems target the handling of lists being one of the most important data structure in functional programming.

Problem 1: Find the last element of a list

Problem 2: Find the last but one element of a list

Problem 3: Find the N-th element of a list

Problem 4: Count the elements in a list

The first element in the list is number 1.

Problem 5: Reverse a list

Problem 6: Check whether a list is a palindrome

A palindrome can be read forward or backward (i.e. 12321)

Problem 7: Flatten a list of lists

Problem 8: Eliminate consecutive duplicates of list elements

Problem 9: Pack consecutive duplicates of list elements into sublists

If a list contains repeated elements they should be placed in separate sublists.

Problem 10: Run-length encoding of a list

Use the result of problem 9 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as tuples (N E) where N is the number of duplicates of the element E.

This post is tagged with scala and programming