This tutorial presents solutions for solving problems 8-11 of the famous 99 Prolog exercises (by Werner Hett). Given a list of integers, we’ll look at different methods to process consecutive duplicates. First, we start with a code snippet on problem#8 then gradually describe a more generalized algorithm using higher order functions to solve these 4 problems in Scala.
I am using the following integer list that contains multiple consecutive duplicates as a basis for testing.
val listdup = List (1,1,1,1,2,2,4,4,5,3,4,4,4,3,3)
Let's start with the first problem where we present two code samples: a hard coded approach and a flexible functional method that enables reusability.