Basic Syntax: Ranges, Lists, and Maps
III. Ranges, Lists, and Maps#
1. Ranges#
Range expressions Ranges use
..<(left-closed, right-open) or..(closed range), with no spaces on either side. Variable expansion is supported.10..<10 # does not include 10 20..10 # includes 10 3a..bRange expressions support stepping:
:step10..6:2 # indicates a step of 2: [0,2,4,6]Can be used for loops, containment checks, array creation, etc.
1let r = 0..8 2 3for i in r {...} # more efficient than looping directly on an array 4r ~: 5 # check if it contains an element 5List.from(r) # convert to an array
2. Lists (Arrays)#
Lists are represented with
[ ]. The internal elements are ordered.They can also be created directly from ranges using
...or...<10...5 # outputs [0,1,2,3,4] 2# equivalent to 3List.from(0..5)
Two dots .. ..< create ranges, while three dots ... ...< create arrays.
Indexing is represented with
.or[i].1let arr = [10, "a", True]Indexing and Slicing
1# Basic indexing 2 3arr.1 4arr[0] # → 10 5 6# Slicing operations 7arr[1:3] # → ["a", True] (left-closed, right-open) 8arr[::2] # → [10, True] (step of 2) 9arr[-1:] # → True (slicing supports negative indexing)Complex Nesting
1# Complex nesting 2[1,24,5,[5,6,8]][3][1] # displays 6 3# Modify element 4# arr[2] = 3.14 # → [10, "a", 3.14]Advanced Operations Refer to the list module.
Edge Cases:
- Array indexing that exceeds bounds will trigger an
out of boundserror. - Array slicing supports negative numbers, indicating indices counted from the end.
- Indexing on non-indexable objects will trigger the following error:
[ERROR] type error: expected indexable type (list/dict/string), found symbol
3. Maps (Dictionaries)#
Maps are represented with
{}1let mydict = {name: "Alice", age: 30} 2 3# Allows shorthand: 4let a = b = 3, 5{a, b} 6{ 7 a, 8 b, 9} # allows trailing commas, allows multi-line writing 10{a, } # trailing comma for a single key-value cannot be omittedDictionary Indexing
1# Basic access 2 3mydict["name"] # → "Alice" 4mydict.name # → "Alice" (shorthand) 5mydict@name # → "Alice" (shorthand) 6 7# Dynamic key support 8let key = "ag" + "e" 9dict[key] # → 30 10 11# Nested access 12let data = {user: mydict} 13data.user.age # → 100Advanced Operations Refer to the map module.
Edge Cases:
| Scenario | Behavior |
|---|---|
| Accessing a non-existent array index | Triggers [ERROR] key x not found in map error |
| Indexing a non-dictionary object | Triggers [ERROR] not valid index option error |
| Indexing an undefined symbol | Returns a string, as operating on filenames is the most common operation in shell |