RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs

lab05-string-indices
string indices
(and, substring)

In yesterday's lab, we fit functions together as if they were lego blocks (in particular, we happened to connect: pickAFile(), makePicture, show). I have also been harping on types — the type given to a function (as part of the question), and its return-type (as the answer). The reason types are important is because they're what are revealing the “shape” of the function(block), which reveals how they can be fit together. The largest software projects are nothing but huge towers of interconnecting function-calls!

Recall how we evaluated makePicture( pickAFile() ) — this worked because makePicture wants to be given a string, and pickAFile returns a string. We had to evaluate the innermost parentheses first: we had to call pickAFile before we knew what string we were going to give to makePicture. So nesting the function-calls (“composing the functions”) forced a sequence on what happened. It may not look like we told the computer to first call pickAFile and then call makePicture, but in essence we really did.

Self-quiz: pickAFile( makePicture() ) makes no sense! Can you give two different reasons why not?

The Editor Pane

The top-half of the JES window is just a text-editor (like Word, except it's slightly savvier about indenting python code).

String indices

substring is a function that takes a big string, a start-index, and a stop-index; it returns the (sub)portion of the string from the start-index up to, but not including, the stop-index. Since we saw substring("hello",1,4) = "ell", we can deduce that 'e' is at index 1, and 'o' (the first character not included) is at index 4.

Indices start counting from 0; we saw the index 1 started from the character 'e'. (Rather than say “the 2nd character”, I'll try to say “the character at index 1”, to avoid confusion.) We say “The indices are 0-based”. This seems weird, but actually makes some sense: a charcter's index is number of chars you need to skip, to reach it.

Staring hw

For the remainder of the class, you can work on hw01—calling functions: types; composing functions.

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Feb.06 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme