Competency 08-1: Sifflet Lists

Identifier: SiffList

Prerequisites: SiffNum, SiffCond

Building and taking apart lists in Sifflet

Learning Objectives

Students should be able to:

  1. Explain the operation of the Sifflet functions head, tail, null, and :
  2. Design, define, test, and interpret Sifflet functions that build and take apart lists.

Lesson

Reading

Sifflet Tutorial, lessons 6 and 7.

Lecture

See Oncourse Module 8.

Practice

Define Sifflet functions:

  1. The parameter is a list with at least three elements. The value is the third element of the list.
  2. The parameter is a non-empty list of non-empty lists. The value is the first element of the first sublist. For example, with parameter [["a", "b"], ["c", "d"]], the value should be "a".
  3. The parameter is anything. The value is a list of three copies of the parameter. For example, with parameter "a", the value should be ["a", "a", "a"].
  4. The parameter is anything. The value is a list containing a single sublist containing a single element, which is the parameter. For example, with parameter 10, the value should be [[10]].

Solutions for some of these:

Assessment: Lab 7

Coding Standards through Chapter 5 apply to this lab. While the coding standards are actually for Python code, apply the spirit of the standards also to Sifflet programs—for example, use descriptive variable and function names.

In addition, although Sifflet allows you to create function and variable names that contain spaces, you should not do so, because such names are confusing. (If your function is named function a and your parameter is named b c, then when you see function a b c doesn't it look like a function with three parameters?)

You'll need to use Sifflet for each of these problems.

Problem 1: Fourth element of a list

Define a Sifflet function with one argument, a list, which returns the fourth element of the list (counting the head as position one).

For example, given the list ["a", "b", "c", "d", "e"], the function should return "d". It is an error if the list contains fewer than four elements. (It is not necessary for your function to check that the list has four or more elements—just let the error happen if you wish.)

Test with any list having four or more elements, and capture a screenshot.

Hint: Non-recursive. Use the head and tail functions.

Problem 2: List of integer quotient and remainder

Define a Sifflet function with two arguments, an integer n and an integer d. The function returns a list with two elements: the integer quotient of n divided by d, and the integer remainder of the same.

Test with the values n = 23 and d = 7. Capture a screenshot.

Hint: Non-recursive. Use cons (:), the empty list (the literal []), div, and mod.