 shmibs's solution

to List Ops in the OCaml Track

Published at Dec 16 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Implement basic list operations.

In functional languages list operations like length, map, and reduce are very common. Implement a series of basic list operations, without using existing functions.

Getting Started

For installation and learning resources, refer to the exercism help page.

Installation

To work on the exercises, you will need Opam and Base. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install base:

opam install base

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test.ml

open Base
open OUnit2

module L = List_ops

let aei exp got _test_ctxt = assert_equal ~printer:Int.to_string exp got

let ael exp got _test_ctxt =
assert_equal exp got ~printer:(fun xs -> String.concat ~sep:";" (List.map ~f:Int.to_string xs))

let is_odd n = n % 2 = 1

let tests = [
"length of empty list">::
aei 0 (L.length []);
"length of normal list">::
aei 4 (L.length [1;3;5;7]);
"length of huge list">::
aei 1_000_000 (L.length (List.range 0 1_000_000));
"reverse of empty list">::
ael [] (L.reverse []);
"reverse of normal list">::
ael [7;5;3;1] (L.reverse [1;3;5;7]);
"reverse of huge list">::
ael (List.range ~start:`exclusive ~stop:`inclusive ~stride:(-1) 1_000_000 0)
(L.reverse (List.range 0 1_000_000));
"map of empty list">::
ael [] (L.map ~f:((+) 1) []);
"map of normal list">::
ael [2;4;6;8] (L.map ~f:((+) 1) [1;3;5;7]);
"map of huge list">::
ael (List.range 1 1_000_001) (L.map ~f:((+) 1) (List.range 0 1_000_000));
"filter of empty list">::
ael [] (L.filter ~f:is_odd []);
"filter of normal list">::
ael [1;3] (L.filter ~f:is_odd [1;2;3;4]);
"filter of huge list">::
ael (List.range ~stride:2 1 1_000_000)
(L.filter ~f:is_odd (List.range 0 1_000_000));
"fold of empty list">::
aei 0 (L.fold ~init:0 ~f:(+) []);
"fold of normal list">::
aei 7 (L.fold ~init:(-3) ~f:(+) [1;2;3;4]);
"fold of huge list">::
aei (List.fold ~init:0 ~f:(+) (List.range 0 1_000_000))
(L.fold ~init:0 ~f:(+) (List.range 0 1_000_000));
"append of empty lists">::
ael [] (L.append [] []);
"append of empty and non-empty list">::
ael [1;2;3;4] (L.append [] [1;2;3;4]);
"append of non-empty and empty list">::
ael [1;2;3;4] (L.append [1;2;3;4] []);
"append of non-empty lists">::
ael [1;2;3;4;5] (L.append [1;2;3] [4;5]);
"append of huge lists">::
ael (List.range 0 2_000_000)
(L.append (List.range 0 1_000_000) (List.range 1_000_000 2_000_000));
"concat of empty list of lists">::
ael [] (L.concat []);
"concat of normal list of lists">::
ael [1;2;3;4;5;6] (L.concat [[1;2];;[];[4;5;6]]);
"concat of huge list of small lists">::
ael (List.range 0 1_000_000)
(L.concat (List.map ~f:(fun x -> [x]) (List.range 0 1_000_000)));
"concat of small list of huge lists">::
ael (List.range 0 1_000_000)
(L.concat
(List.map ~f:(fun x -> List.range (x*100_000) ((x+1)*100_000))
(List.range 0 10)))
]

let () =
run_test_tt_main ("list-ops tests" >::: tests)
let rec fold ~init:acc ~f:f l =
match l with
| [] -> acc
| h :: t -> fold ~init:(f acc h) ~f:f t

let length l =
fold l ~init:0 ~f:(fun i _ -> i + 1)

let reverse l =
fold l ~init:[] ~f:(fun l e -> e :: l)

let map ~f:f l =
fold l ~init:[] ~f:(fun accl e -> (f e) :: accl)
|> reverse

let filter ~f:f l =
fold l ~init:[] ~f:(fun accl e ->
if f e then e :: accl
else accl
)
|> reverse

let append l1 l2 =
reverse l1 |> fold ~init:l2 ~f:(fun l e -> e :: l)

let concat ll =
fold ll ~init:[] ~f:(fun accl sl ->
fold sl ~init:accl ~f:(fun accl e -> e :: accl)
)
|> reverse