Merge pull request #323 from aeternity/fromtostep

Add check in from_to_step
This commit is contained in:
Hans Svensson 2021-07-05 09:39:32 +02:00 committed by GitHub
commit 5784f074a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View File

@ -1216,7 +1216,7 @@ List.unzip(l : list('a * 'b)) : list('a) * list('b)
Opposite to the `zip` operation. Takes a list of pairs and returns pair of lists with respective elements on same indices. Opposite to the `zip` operation. Takes a list of pairs and returns pair of lists with respective elements on same indices.
### merge #### merge
``` ```
List.merge(lesser_cmp : ('a, 'a) => bool, l1 : list('a), l2 : list('a)) : list('a) List.merge(lesser_cmp : ('a, 'a) => bool, l1 : list('a), l2 : list('a)) : list('a)
``` ```
@ -1224,7 +1224,7 @@ List.merge(lesser_cmp : ('a, 'a) => bool, l1 : list('a), l2 : list('a)) : list('
Merges two sorted lists into a single sorted list. O(length(l1) + length(l2)) Merges two sorted lists into a single sorted list. O(length(l1) + length(l2))
### sort #### sort
``` ```
List.sort(lesser_cmp : ('a, 'a) => bool, l : list('a)) : list('a) List.sort(lesser_cmp : ('a, 'a) => bool, l : list('a)) : list('a)
``` ```

View File

@ -80,6 +80,7 @@ namespace List =
* `b` only if `(b - a) mod step == 0`. `step` should be bigger than 0. * `b` only if `(b - a) mod step == 0`. `step` should be bigger than 0.
*/ */
function from_to_step(a : int, b : int, s : int) : list(int) = function from_to_step(a : int, b : int, s : int) : list(int) =
require(s > 0, "List.from_to_step: non-positive step")
from_to_step_(a, b - (b-a) mod s, s, []) from_to_step_(a, b - (b-a) mod s, s, [])
private function from_to_step_(a : int, b : int, s : int, acc : list(int)) : list(int) = private function from_to_step_(a : int, b : int, s : int, acc : list(int)) : list(int) =
if(b < a) acc if(b < a) acc