rauschma / exploring-reasonml

http://reasonmlhub.com/exploring-reasonml/
26 stars 0 forks source link

Chapter: Lists #13

Open rauschma opened 6 years ago

geraldodev commented 6 years ago

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 commented 6 years ago

/**

eugrdn commented 6 years ago

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?

rauschma commented 6 years ago

@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 commented 6 years ago

@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

njgarg22 commented 5 years ago
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.

PhiLhoSoft commented 5 years ago

Typo at:

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

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