-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
-- # Comparing arrays for equality | ||
|
||
-- To compare two one-dimensional arrays for equality, we can compare | ||
-- them elementwise, yielding an array of `bool`s, then use `and` to | ||
-- check whether all those elements are true: | ||
|
||
def eq_arr_i32 [n] (a: [n]i32) (b: [n]i32) : bool = | ||
and (map2 (==) a b) | ||
|
||
-- This function requires us to already know that the arrays have the | ||
-- same size. We can also write a variant that does not have this | ||
-- requirement, but instead checks for the size explicitly and then | ||
-- uses a [size coercion](size-coercions.html): | ||
|
||
def eq_arr_i32_any [n] [m] (a: [n]i32) (b: [m]i32) : bool = | ||
n == m && eq_arr_i32 a (b :> [n]i32) | ||
|
||
-- This implementation is still restricted to arrays of integers. We | ||
-- can make a polymorphic one that expects the user to provide an | ||
-- equality function: | ||
|
||
def eq_arr 'a [n] [m] (eq: a -> a -> bool) (a: [n]a) (b: [m]a) : bool = | ||
n == m && and (map2 eq a (b :> [n]a)) | ||
|
||
-- This can then be used to define a function that works for | ||
-- two-dimensional arrays: | ||
|
||
def eq_arr_2d 'a [n1] [n2] [m1] [m2] | ||
(eq: a -> a -> bool) (a: [n1][n2]a) (b: [m1][m2]a) : bool = | ||
n1 == m1 && n2 == m2 && eq_arr (eq_arr eq) a (b :> [n1][n2]a) | ||
|
||
-- ## See also | ||
-- | ||
-- [Searching](searching.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- # Integer logarithm | ||
-- | ||
-- The base-2 integer logarithm can be computed by counting the number | ||
-- of leading *zero bits* in the integer, through the `clz` builtin | ||
-- function. For example for 64-bit integers: | ||
|
||
def ilog2 (n: i64) : i64 = i64.i32 (63 - i64.clz n) | ||
|
||
-- > ilog2 127 | ||
|
||
-- > ilog2 128 | ||
|
||
-- ## See also | ||
-- | ||
-- [Merge sort](merge-sort.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters