Skip to content

Commit 19fb576

Browse files
added Fact check for Head/Tail (#451)
Added a fact check for Head/Tail array functions, which improves a bit the feedback. ``` let foo : Int[] = []; let bar = Head(foo); ``` current error: `ERROR: runtime error: index out of range: 0` new error: `ERROR: runtime error: program failed: Array must have at least 1 element` ``` let foo : Int[] = []; let bar = Tail(foo); ``` current error: `ERROR: runtime error: value cannot be used as an index: -1` new error: `ERROR: runtime error: program failed: Array must have at least 1 element` --- Regarding for the trailing whitespace changes - VS Code cleaned it up "on its own" due to `"editor.formatOnType": true` setting - but I kept them because they seem anyway to be consistent with the rest of the codebase. Co-authored-by: DmitryVasilevsky <[email protected]>
1 parent c1f579f commit 19fb576

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

library/std/arrays.qs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace Microsoft.Quantum.Arrays {
155155
///
156156
/// # Example
157157
/// ```qsharp
158-
/// let evensCount = Count(x -> x % 2 == 0, [1, 3, 6, 7, 9]);
158+
/// let evensCount = Count(x -> x % 2 == 0, [1, 3, 6, 7, 9]);
159159
/// // evensCount is 1.
160160
/// ```
161161
function Count<'T>(predicate : ('T -> Bool), array : 'T[]) : Int {
@@ -371,7 +371,7 @@ namespace Microsoft.Quantum.Arrays {
371371
function FlatMapped<'TInput, 'TOutput>(mapper : ('TInput -> 'TOutput[]), array : 'TInput[]) : 'TOutput[] {
372372
mutable output = [];
373373
for element in array {
374-
set output += mapper(element);
374+
set output += mapper(element);
375375
}
376376
output
377377
}
@@ -482,6 +482,7 @@ namespace Microsoft.Quantum.Arrays {
482482
/// # Output
483483
/// The first element of the array.
484484
function Head<'A> (array : 'A[]) : 'A {
485+
Fact(Length(array) > 0, "Array must have at least 1 element");
485486
array[0]
486487
}
487488

@@ -1041,7 +1042,7 @@ namespace Microsoft.Quantum.Arrays {
10411042
/// ```qsharp
10421043
/// let sortedArray = Sorted(LessThanOrEqualI, [3, 17, 11, -201, -11]);
10431044
/// ```
1044-
///
1045+
///
10451046
/// # Remarks
10461047
/// The function `comparison` is assumed to be transitive, such that
10471048
/// if `comparison(a, b)` and `comparison(b, c)`, then `comparison(a, c)`
@@ -1059,7 +1060,7 @@ namespace Microsoft.Quantum.Arrays {
10591060
// Sort each sublist, then merge them back into a single combined
10601061
// list and return.
10611062
SortedMerged(
1062-
comparison,
1063+
comparison,
10631064
Sorted(comparison, left),
10641065
Sorted(comparison, right)
10651066
)
@@ -1093,7 +1094,7 @@ namespace Microsoft.Quantum.Arrays {
10931094
/// array that match the given locations.
10941095
///
10951096
/// # Remarks
1096-
/// If `locations` contains repeated elements, the corresponding elements
1097+
/// If `locations` contains repeated elements, the corresponding elements
10971098
/// of `array` will likewise be repeated.
10981099
///
10991100
/// # Type Parameters
@@ -1109,9 +1110,9 @@ namespace Microsoft.Quantum.Arrays {
11091110
/// # Output
11101111
/// An array `out` of elements whose locations correspond to the subarray,
11111112
/// such that `out[index] == array[locations[index]]`.
1112-
///
1113+
///
11131114
/// # Example
1114-
///
1115+
///
11151116
/// ```qsharp
11161117
/// let array = [1, 2, 3, 4];
11171118
/// let permutation = Subarray([3, 0, 2, 1], array); // [4, 1, 3, 2]
@@ -1210,7 +1211,9 @@ namespace Microsoft.Quantum.Arrays {
12101211
/// # Output
12111212
/// The last element of the array.
12121213
function Tail<'A> (array : 'A[]) : 'A {
1213-
array[Length(array) - 1]
1214+
let size = Length(array);
1215+
Fact(size > 0, "Array must have at least 1 element");
1216+
array[size - 1]
12141217
}
12151218

12161219
/// # Summary

0 commit comments

Comments
 (0)