Lab Goals and Policies
Labs are an opportunity to work together with a partner on exercises to reinforce concepts from lecture and to prepare for problems on the problem sets. A successful lab experience requires each member to contribute equally. In pair programming, one student is the "driver", who controls the keyboard and mouse. The other is the "navigator", who observes, asks questions, suggest solutions, and thinks about slightly longer-term strategies. The two programmers switch roles about every 20 minutes. If you believe your partner is not participating appropriately in pair programming please first address your concerns to your partner, and try to agree on what should be done to make the pair programming more successful. If that approach is not successful, explain the issues to one of your instructors, who will work with you and your partner to improve the situation.
Getting Started
To get started, open up a new file in the SuperCollider IDE.
Expressions
Consider the following expressions in sclang below and predict the result of those expressions. Recall from lecture that SuperCollider’s language evaluates operators left to right. Normal operator precedence in most languages does not apply! Note that some of these expressions may result in errors.
1 + 2 * 3
3 < 7 && true
1 - 4 * 5 - 8
3 < 7 || 4 < 8
Confirm your answers by copying and pasting the code into the SuperCollider IDE. Run each line of code by clicking on the line and hitting Command + Return on a Mac or CTRL + Return on a Windows PC.
Simple Functions
Below write simple functions. The point here is to get practice with handling the notation, parameters, return values, and body of functions with sclang.
-
Write a function called
~addFive
that takes an argument callednum
and returnsnum
plus five. -
Write a function called
~addPrintFive
that takes an argument callednum
and returnsnum
plus five and posts that result to post window. Be careful here. The post window in SuperCollider will also show the return value of an expression. SuperCollider distinguishes between posted values and returned values by prepending the text “->” in front of return values. -
Write a function called
~pluralize
that takes a string and returns that string plus"s"
. The++
operator concatenates two strings and the+
operator concatenates two strings and puts a space between them. -
Write a function called
~coolOff
that takes a temperature and prints “Find some shade” if the temperature is above 80 and prints “It’s not hot” otherwise. The function can return anything. Every function call in SuperCollider must return a value. As a reminder, a function call returns the last expression evaluated.
More Functions
-
Write a function called
~endsInS
that takes a string and returnstrue
if the string ends in the character “s” or “S” andfalse
otherwise. You may assume the string has at least one character. A string in SuperCollider is an array of characters. You can use the.last
method to get the last character in the string.Sample calls:
~endsInS.value("strings"); // should return `true` ~endsInS.value("weird"); // should return `false` ~endsInS.value("CATS"); // should return `true`
-
Write a function called
~countPlurals
that takes an array of strings and returns the number of strings in the array that end in “s” or “S”. You should use the function~endsInS
you wrote above.Sample calls:
~countPlurals.value(["cats", "dogs", "mouse", "hens"]); // should return 3 ~countPlurals.value(["note", "pen"]); // should return 0
-
Write a function called
~longestWord
that takes a list of words and returns the string from the list with the longest length. To get the length of a string in sclang, use the method.size
. If there are multiple strings with the longest length, return any one of them.Sample calls:
~longestWord.value(["water", "wind", "welcome", "wishes"]); // should return "welcome" ~longestWord.value(["Line", "Limb"]); // should return either "Line" or "Limb"
-
Write a function called
~checkPassword
that takes a string and returnstrue
if the string meets the following criteria andfalse
otherwise.- Has at least one upper case letter and one lower case letter
- Has at least one digit
- Contains at least one of these special characters: !#$%&*
Sample calls:
~checkPassword.value("An12$"); // true ~checkPassword.value("76Hu"); // false ~checkPassword.value("loveMyDog123"); // false ~checkPassword.value("ok8!"); // false ~checkPassword.value("musicIsTheBest87!!!"); // true
Reading Documentation
Reading documentation is an important skill as a programmer. Documentation for a language details all the types, constructs, and nuances of the language. Memorizing the basics of any language is important but many tasks require examining the documentation. In these exercises, I will ask you to complete some tasks using methods and objects we have not dicussed in class. You should refer to the documentation in the Help Browser. A good way to start is by looking up the particular class you are working with. For example, if the question revolves around arrays, spend some time reading over the Array class documentation.
Objects in SuperCollider inherit methods from their parent objects. For example, the Array class inherits methods from ArrayedCollection which inherits from SequenceableCollection which inherits from Collection which inherits from Object. Therefore, the Array class has all the methods from those other classes as well. The top of the documentation for a particular class will always detail the inheritance tree. Importantly, this means that the solution to a problem in the Array class actually could be found in a method detailed in the Collection class. It’s generally a good idea to read through the documentation of each parent class.
In these next few problems, you will write a single line of code that uses a method found in the documentation to solve each problem. The first one is done as an example.
DO NOT USE ANY LOOPS TO SOLVE THESE PROBLEMS
-
Write a single line of code that takes the string stored in the variable
string
and uses it in an expression that evaluates totrue
if the first letter equals the last letter andfalse
otherwise. Here we will account for case. Therefore, a string likeSnakes
isfalse
.( var string = "ALABAMA"; // Try with different strings // Your code here )
-
Write a single line of code that splits the string stored in the variable
string
and splits it by space (i.e., “ “) into a list of words.( var string = "This sentence has some spaces."; // Your code here )
-
Write a single line of code that selects a random number from an array of numbers stored in the variable
arr
.( var arr = [4, 5, 0, -3, 10]; // Your code here )
-
Write a single line of code that creates an array of numbers starting from 5 up to 500 by 5. Here we do not need any other object stored in a variable.
Solutions
Solutions to the lab will be posted after the lab is over.
lab_sclang_basics_solns.scd