Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercises

Exercise 1: Basic List Operations

Create a list of integers from 1 to 10. Then, perform the following tasks:

  1. Extract the first element.
  2. Extract the last element.
  3. Get all elements except the first.
  4. Get all elements except the last.
  5. Check if the list contains the number 5.

Exercise 2: Concatenation and Addition

  1. Create two lists: one with even numbers from 2 to 10, and another with odd numbers from 1 to 9.
  2. Concatenate these lists into a single list.
  3. Add the number 0 to the beginning of the concatenated list.

Exercise 3: Mapping and Filtering

Given a list of numbers from 1 to 10:

  1. Multiply each number by 2.
  2. Filter the resulting list to keep only numbers greater than 10.

Exercise 4: Folding and Reducing

  1. Create a list of integers from 1 to 5.
  2. Compute the sum of all elements using foldLeft.
  3. Compute the product of all elements using reduce.

Exercise 5: Using ListBuffer

  1. Create a mutable ListBuffer and add elements 1, 2, and 3 to it.
  2. Append the element 4 to the list buffer.
  3. Remove the element 2 from the list buffer.
  4. Convert the ListBuffer to an immutable list.

Exercise 6: Working with Arrays

  1. Create an array of integers from 1 to 5.
  2. Update the third element (index 2) to 10.
  3. Print the length of the array.
  4. Iterate over the array and print each element.

Exercise 7: Zipping and Unzipping

  1. Create two lists: one with numbers 1 to 3, and another with the strings "one", "two", and "three".
  2. Zip these lists together.
  3. Unzip the zipped list back into two separate lists.

Exercise 8: Using Range to Create Lists

  1. Create a list of numbers from 1 to 10 using Range.
  2. Create a list of even numbers from 2 to 20 using Range.
  3. Create a list of numbers from 10 to 1 (in reverse order) using Range.

Exercise 9: LazyList

  1. Create a LazyList of the first 10 positive integers.
  2. Define a LazyList that generates an infinite sequence of even numbers.
  3. Take the first 10 elements from the infinite LazyList and print them.

Exercise 10: Grouping and Partitioning

  1. Create a list of integers from 1 to 10.
  2. Group the integers by whether they are even or odd.
  3. Partition the list into two lists: one containing numbers less than or equal to 5, and another containing numbers greater than 5.