Lists, Vectors, and Maps

General

This article assumes that you have read  Creating Temporary Variables.

At times we want to have a series of data that is grouped together. Many of the variables provided by LoanPro are groups of data. This allows us to easily keep track of large groups without having to have a separate variable for each item in the group. In Lisp, there are several types of groups, each with their own properties. This goes over the different types of groups and how to use them.  Please note that if you change a group that’s assigned to a variable, it won’t change what’s stored in that variable. That means if you have a group of numbers stored in x and you remove a number from the group, x will still hold the original group.

Lists

To create a list, use the list operator and then list all of the items you want in the list. You can have as many items in the list as you want. Below is an example of creating a list

(list 1 2 3 4)

You can also assign a list to a temporary variable. Below assigns the above list to the variable x.

(let [x (list 1 2 3 4)] x)

Vectors

Vectors allow you to access elements of data by a numerical index. Vectors have a lot of built-in functions which can help you use them. To declare a vector, you just need to surround the items you want in a vector inside of square brackets as shown below:

[1 3 8 0 49 12]

You can also assign a vector to a variable, as shown below:

(let [x [1 2 3 4]] x)

Getting items from a Vector

To get items from a vector at a specified index, you use the vector as the operator and you have the index be the operand. The following example gets the second item from the vector “x”.

(let [x [1 2 3 4]] (x 2))

To get the first item from the vector, you can use the first operator with the vector as the operand. The example below evaluates to 1:

(first [1 2 3 4])

To get the last item from the vector, you can use the last operator with the vector as the operand. The example below evaluates to 4:

(last [1 2 3 4])

Maps

Maps allow associating values with an index (sometimes called a “key”). This means that I can have a :firstname index that has the value of the first name of someone, and I can have a :lastname index that has the last name of someone. Maps are created by using curly braces and by having the format {index1 value index2 value}. Since you get to assign the indexes, it doesn’t matter what the indexes are as long as you remember them and you don’t have two of the same indexes.

Below is a sample of declaring a map:

{:a 1 :b 2}

The “:a” and the “:b” create named indexes. With the colon in front, Lisp won’t try to replace it with a variable name or with text, but instead creates a named index. This allows us to create indexes which are easier to read (such as :firstname). Additionally, maps can be assigned to variables as follows:

(let[x {:a 1 :b 2}] x)

Getting items from a Map

To get a value at an index we can use the get operation. For the get operation, we pass in the map as the first operand and the index as the second operand, as follows:

(let[x {:a 1 :b 2}] (get x :b))

The above example returns 2. Additionally, we can get items at an index the same way we do it with vectors. Below returns the same result

(let[x {:a 1 :b 2}] (x :b))

To get a list of indexes we use the “keys” operation. It only takes one operand which is a map. This will return a list of keys used by the map. The example below will return (:a :b)

(let[x {:a 1 :b 2}] (keys x))

To get a list of all of the values in the map we use the “vals” operation, which takes a map as an operand. This will return a list of all values contained in the map. The below will return (1 2)

(let[x {:a 1 :b 2}] (vals x))

Removing items from Maps

To remove items from a map, you use the dissoc operation. The dissoc operation takes the map to use as the first operand, and then as many indexes of items to remove as you want for the rest of the operands. Please note that this does not affect the original map and just creates a copy of the map without the specified items. Below is an example:

(let[x {:a 1 :b 2}] (dissoc x :a))

Adding Items To Groups

Please note that these operations don’t affect the original group; they just create a duplicate group that has the changes applied.

Adding items

To add items to a list or vector you use the “conj” operation. The first operand is the list/vector to add the elements to, then the rest of the operand (however many you want) are the items to add to the list/vector. This is shown below:

(let [v (list 1 2 3 4)] (conj v 3 4 5))

For maps, you use the “assoc” operation. The first operand is the map, and then you do a series of the new index and the new value. The syntax is (conj map index1 value1 index2 value2 …). Below is an example

(let[x {:a 1 :b 2}] (assoc x :c 3 :d 4))


How did we do?


Powered by HelpDocs (opens in a new tab)