How do you take a slice of a list in OCaml/ReasonML?
Asked Answered
M

5

5

For example in Ruby you could do something like:

list = ["foo", "bar", "baz", "qux", "quux", "corge"]
result = list[2..4]

And result would contain ["baz", "qux", "quux"].

How would you do this in OCaml/ReasonML?

Myriagram answered 26/10, 2018 at 19:21 Comment(2)
use pattern matching or a library with give and take #2710733Simmons
for reason use List.hd and List.tk reasonml.github.io/api/List.html , Array module has subSimmons
S
5

There is no in built function for slicing list, but can be done easily. Since we have a start point and an end point, we can break down the problem in two parts. First part is to drop a few elements till we reach the starting point and second part is to take few elements from the start point till the end point.

let rec drop = (n, list) =>
  switch (list) {
  | [] => []
  | [_, ...xs] as z => n == 0 ? z : drop(n - 1, xs)
  };

let rec take = (n, list) =>
  switch (list) {
  | [] => []
  | [x, ...xs] => n == 0 ? [] : [x, ...take(n - 1, xs)]
  };

now that we have these two functions, we can combine them to drop initial elements from till start point drop(i, list) and then pass this new list to take elements from start point to end point

take(k - i + 1, drop(i, list));

in total

let slice = (list, i, k) => {

  let rec drop = (n, list) =>
    switch (list) {
    | [] => []
    | [_, ...xs] as z => n == 0 ? z : drop(n - 1, xs)
    };

  let rec take = (n, list) =>
    switch (list) {
    | [] => []
    | [x, ...xs] => n == 0 ? [] : [x, ...take(n - 1, xs)]
    };

  take(k - i + 1, drop(i, list));
};

A better approach would be to provide starting point and then range rather than end point because here we don't constraint that end point should be bigger than starting point

let slice = (list, start, range) => {

  let rec drop = (n, list) =>
    switch (list) {
    | [] => []
    | [_, ...xs] as z => n == 0 ? z : drop(n - 1, xs)
    };

  let rec take = (n, list) =>
    switch (list) {
    | [] => []
    | [x, ...xs] => n == 0 ? [] : [x, ...take(n - 1, xs)]
    };

  take(range, drop(start, list));
};
Shrieve answered 8/11, 2018 at 12:47 Comment(0)
P
2

If you have access to bucklescript's Belt libraries, you could do something like:

open Belt;

let myList = ["first", "second", "third", "fourth", "fifth", "sixth"];

/* To get 2..4 */
myList
  ->List.drop(2)
  ->Option.getWithDefault([])
  ->List.take(3)
  ->Option.getWithDefault([])
  ->Js.log;

/* Gives you the list ["third", "fourth", "fifth"] */
Periphrasis answered 30/10, 2018 at 7:23 Comment(0)
S
2

Use

List.filteri (fun i _ -> i >= start && i <= end)
Signatory answered 19/12, 2020 at 22:41 Comment(1)
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Frugal
S
1

There is no special language notation for OCaml slicing. You can write your function, say using the pattern matching, or combine head with take functions (those are available in standard libraries). For Reason combine List.hd and List.tk https://reasonml.github.io/api/List.html , also Array module has a sublist Array.sub. The OCaml was discussed here how to get a sub list from a list in ocaml

Simmons answered 26/10, 2018 at 19:38 Comment(0)
S
1

If you have access to BuckleScript, you can use:

let list = ["foo", "bar", "baz", "qux", "quux", "corge"];

let sliced = Js.Array.slice(~start=2, ~end_=4, list);

See more in the BuckleScript docs

Sebastien answered 28/9, 2019 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.