Range Over Built in Prolog
Range over Built-in Types
range iterates over elements in a variety of built-in data structures. Let’s see how to use range
with some of the data structures we’ve already learned.
Summing numbers in a list
Here we use for
to sum the numbers in a list. Arrays work like this too.
for
on lists provides both the index and value for each entry. Above we didn’t need the index, so we just iterated directly over the values. Sometimes we actually want the indexes though.
Iterating over key/value pairs in dictionaries
for
on dictionaries iterates over key/value pairs.
Iterating over just the keys of a dictionary
for
can also iterate over just the keys of a dictionary.
Iterating over characters in a string
for
on strings iterates over Unicode code points. The first value is the starting byte index of the character and the second the character itself.