Kieliteknologian oppiaine

Ohjelmoinnin perusteet: Ctl220 2003s

- KIT-verkosto | Yleisen kielitieteen laitos | Kieliteknologian opetus| Helsingin yliopisto -
Kurssin sivut
Kurssin kalenteri
Kurssin materiaali
Opettajan kotisivu
Kurssin kuvaus

Kurssin materiaali

On-line Resources

  • Perl Tutorial by Nik Silver (University of Leeds). The original Perl tutorial. Good, but it assumes you know some C programming, and it's now out of date.
  • Perl Tutorial by Chris Manning (University of Sydney). Adapted from Nik Silver's tutorial, with more explanations for beginners. Good linguistics-based examples, but using MacPerl.
  • Perl Programming Course for Bioinformatics and Internet (Weizmann Institute). A good on-line course with clear explanations, but the examples are about Bioinformatics.
  • Introduction to Programming by Stephen Fitzpatrick (Queen's University of Belfast). An excellent tutorial, but the programming language is Pascal. It's good to see a different programming language, because we should aim to learn programming, not just details of Perl.

1. Getting started. Algorithms.

2. Variables and Assignment (Sequence).

  • Lecture notes: Introduction to Perl and Scalar Data (Weizmann Institute)
  • Practical work: Variables
    • Read Scalar Variables in Chris Manning's tutorial and do the exercise.
    • Write a program which assigns $a = 2 and $b = 5 and prints out their values: "$a is 2 and $b is 5". Make sure the variable names are printed correctly including the $ symbol.
    • Modify the program so that it assigns $a = 2 and $b = 5 and prints out their values: "$a is 2 and $b is 5", then swaps their values and prints their values again: "$a is 5 and $b is 2".
    • No assignment this week, but read Scalar Data again carefully.

3. Arrays and Foreach-Loops.

  • Lecture notes: Arrays (Weizmann Institute)
  • Practical work: Arrays
    • Read Array Variables (Manning).
    • The section on Array Assignments includes an example:
      ($a, $b) = ($c, $d); # Same as $a=$c; $b=$d;
    • Based on this example, can you find another way to do last week's practical work, swapping the values of $a and $b?
  • Assignment 2: Arrays

4. User-defined Functions (Subroutines).

  • Lecture notes: Built-in Functions and Functions (Weizmann Institute)
    • An old style of subroutines is described in Subroutines (Silver). Be aware of the old style, but always use the modern style yourself. It's clearer with less risks of errors.
    • Always assign the input parameters to a list of variables at the start of the subroutine.
    • Always declare variables with my, to avoid any side-effects outside the subroutine.
    • Always use an explicit return statement to return the correct value.
    • Always invoke subroutines using parentheses: subroutine(); rather than &subroutine;.
  • Practical work: User-defined Functions
    • Do the examples in Functions.
    • Write a function that does the first task in Assignment 2 (print array items with spaces). Write a program that invokes the function using @alphabet. Include the function definition at the end of the program.
    • Write more functions to do the other tasks in Assignment 2 (print array items with newlines, return number of items in the array, return a string "From $first to $last."). Add them to the program and invoke them with @alphabet.

5. Scope. Diagnostics.

  • Lecture notes: Writing Safe Code (Weizmann Institute)
  • Practical work: Diagnostics.
    • Do the examples in Writing Safe Code, including the deliberate mistakes.
      Make sure you get the diagnostic warnings. Do you understand why?
    • Add use diagnostics; to last week's practical work on user-defined functions.
      Insert some deliberate mistakes - for example, spell $first incorrectly as "$frist". Check the diagnostic warnings. Try some other errors.
  • Assignment 3: Functions

6. If-Then-Else Conditions (Selection).

7. For-Loops and While-Loops (Iteration).

  • Lecture notes: Do-While Control Structure (+audio) (scsite.com)
    • Loops in Pascal (Fitzpatrick)
    • Control Structures in Perl, sections 5 - 10 (Weizmann Institute).
    • When to use FOR and when to use WHILE? In general:
      Use FOR when you do something to all the items (in an array or in a file).
      Use WHILE when you search for one special item amongst the other items.
  • Practical work: Loops.
    • "This isn't rocket science". What does this program do? Try running it.
    • Write a Perl program that creates an array of English number names from "one" to "ten", then uses a for-loop to do a countdown from "ten" to "one".
    • Make another version of your program. Change the array of number names to Finnish, but don't change the for-loop. The program should now do the countdown in Finnish.
  • Assignment 4: Control Structures

8. Program Design.

  • Lecture notes: Johnson Chapter 3: Writing Programs.
  • Practical work: The mathq program.
    • Copy the design and code to your directory. There is a syntax error and a semantic error in the code. Fix them so that the program works according to the specification. Call this version 1.1.
    • When the program generates the question "0 / 0 = ?", why is the first number zero? Design a version of the program which does not generate zero division questions. You only need to change one line. Modify the design, modify the code, and test it. Call this version 1.2.
    • Design another version which keeps a running total score of right and wrong answers. Start from version 1.2, modify the design, modify the code, and test it. Call this version 1.3.
    • Here is a version of the mathq program with subroutines. Test it and fix the errors (including zero divide). Make a new version which keeps the running total of right and wrong answers.

9. Files and Input/Output.

10. Text Processing.

11. Regular Expressions.

12. Algorithms and Data Types (Recursion).


© Graham Wilcock 2003.