exploring-reasonml icon indicating copy to clipboard operation
exploring-reasonml copied to clipboard

Chapter: Lists

Open rauschma opened this issue 7 years ago • 7 comments

rauschma avatar Feb 07 '18 06:02 rauschma

No need rec on summarize that uses fold_left

 let rec summarize = (l: list(int)) =>
   ListLabels.fold_left(~f=(r, elem) => r + elem, ~init=0, l);

geraldodev avatar Feb 21 '18 13:02 geraldodev

/**

  • Inside the list l, remove all occurrences of the value ~value
  • with the value ~with_. */ The replace comment retained the remove from the previous example. I think it is : replace all occurrences...

geraldodev avatar Feb 21 '18 13:02 geraldodev

Guys, could you help to understand the phrase: "Note that concatenating lists is comparatively slow, because you must prepend each element of the first operand to the second operand..". Is that makes sense how it slow or quick the operation, if it will become the common js operation after the OCaml to JS compilation? I mean that we do concatenation in js by the Array.concat, and is that exactly this method we will use in compiled to js code?

eugrdn avatar Apr 01 '18 17:04 eugrdn

@geraldodev Could you put lines with three backticks before and after your code, like this:

```
this is the code
```

Currently your comments are hard to read and understand (GitHub lets you edit existing comments).

rauschma avatar Apr 01 '18 18:04 rauschma

@eugrdn I take it you understand why the operation is slow?

If you concatenate often, it’s better to switch to arrays. ReasonML also lets you convert between lists and arrays, so you can use each of them for what they do well, as described in the following table: http://reasonmlhub.com/exploring-reasonml/ch_arrays.html#lists-vs.arrays

rauschma avatar Apr 01 '18 18:04 rauschma

let rec getElementAt = (~index: int, l: list('a)) =>
  switch l {
  | [] => None
  | [head, ..._] when index <= 0 => Some(head)
  | [head, ...tail] => getElementAt(~index=index-1, tail)
  };

Js.log(getElementAt(-7, [5,6,7])); //gives 5

Isn't the above operation should also result in None? Then the condition should be index == 0.

njgarg22 avatar Feb 14 '19 16:02 njgarg22

Typo at:

type mylist('a) =
  | Nil;
  | Cons('a, mylist('a))

The semi-colon should be on the last line, not after Nil.

PhiLhoSoft avatar May 28 '19 15:05 PhiLhoSoft