CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #18

PROGRAMMING IN PASCAL:
TEXT PROCESSING (continued)

(3.  The Post Correspondence Problem -- continued)

c)  Suppose:

    OP1(S) = Saaba
    OP1(T) = Taab

    OP2(S) = Sabb
    OP2(T) = Tbaab

    OP3(S) = Sbbab
    OP3(T) = Tabba

Now is there a sequence of operations 1-3 applied
to initially empty S & T such that eventually S=T?

Answer:  No!

d)    There's nothing special about these examples.
        There are Post Correspondence Problems with
        different operations, or with different numbers
        of operations.  (For more info, see the
        Directory of Documents.)

e)    Now consider the following problems:

        Given 2 initially-empty strings,
        and given a set of concatenation operations,
        is there an algorithm that outputs "yes"
        if the 2 strings eventually correspond ("match")?

        *    The answer is:  yes, there is.

        Is there such an algorithm that outputs "no"
        if there is no match?

        *    The answer is:  maybe.
                It depends on whether it could
                show an impossibility.
                Sometimes that can be done:

                OP1(S) = Sa
                OP1(T) = Tb

                will never yield a match.

        Is there such an algorithm that outputs
        "yes" if there is a match and also "no" if
        there isn't?

        *    Here, the answer is:  no.

4.  Algorithms and Computer Programs

a)    Recall that an algorithm is an unambiguous
       & effective (i.e., it is correct & it halts)
       procedure for solving a problem

        A program is an expression of an algorithm
        in a programming language.

        The same algorithm can be expressed in
        different programming languages (and, in
        fact, any algorithm can be expressed in any
        programming language, although some
        languages are better for some algorithms
        (for instance, a Karel algorithm is easier to
        express in Karel's own language, while a
        word-processing program would be very
        difficult (but not impossible!) to express
        in Karel's language))

        For those of you who think that we should
        be learning Java instead of Pascal, here are
        two programs for the same algorithm, one
        in each language.  You tell me which is
        easier to understand:

        Pascal:

            program HelloWorld;
            begin
              writeln('Hello World!');
              writeln('This is a simple Pascal test.')
            end.

        Java:

import java.awt.*;
public class HelloWorld extends java.applet.Applet
{
 TextField m1,m2;
 public void init()
 {
  m1 = new TextField(60);
  m2 = new TextField(60);
  m1.setText("Hello World!");
  m2.setText("This is a simple Java test.");
  add(m1);
  add(m2);
 }
}

b)    A problem is computable =def
            there is an algorithm that solves it

c)    Open issue:
        What problems are algorithmically solvable?

       Biermann, p. 43, says, "a beautiful piece of music"
        is not computable.

        *    This is probably true, but not because music
                is not computable, but because "beauty"
                is too vague a notion.

        *    The first edition of his book said that to
            "write a musical composition" was not
            computable.  But many faculty and students
            at UB have written computer programs that,
            in turn, write music:  Lejaren Hiller, Charles Ames,
            and Kemal Ebcioglu.
            (see also this link on Lejaren Hiller)

        *    A better question (& an AI research issue):
                Can (or, is) "writing (beautiful) music" (be)
                computable?
 

5.    The Assignment Statement

   Sometimes, it's tricky to translate an idea for
   an algorithm into a program.

    simple e.g.)    How to sway info in 2 locations
                            (e.g., your 2 hands)
                        e.g.) my lunch & my briefcase

    solution:    Need a third hand (third location):

    e.g.) Suppose we have 2 memory locations,
            named "lefthand" and "righthand", with
            "lefthand" containing my briefcase,
            and "righthand" containing my lunch,
            & suppose we want to swap their contents.
 
lefthand
briefcase
righthand
lunch
    We need an assignment statement:

        syntax:
            memloc1 := memloc2

        semantics:
            contents of memloc1 are replaced
            by a copy of contents of memloc2

        i.e.)                    before        after
                memloc1         ?               A
                memloc2         A               A

    So, to swap lefthand's contents & righthand's
    contents, use this algorithm:

    1.    temploc := lefthand
    2.    lefthand := righthand
    3.    righthand := temploc

    Here's what happens in memory when this
    algorithm is executed:

            temploc    lefthand    righthand
     0.         ?            briefcase    lunch
     1.     briefcase    briefcase    lunch
     2.      briefcase    lunch        lunch
     3.      briefcase    lunch        briefcase
 
 

6.  Concatenation

Now, we'll use the assignment operator
 
            :=

to explore the text-processing operation of
concatenation.

a)    Let s1, s2 be strings
        e.g.) var s1, s2 : varying [20] of char;

        Then the concatenation of  s1 with  s2 =def
                        s1s2

    i.e.)    to concatenate 2 strings is to write the
             first and then write the second next to it,
            with no spaces in between.

b)    In our dialect of Pascal, we can either say:

            s1 + s2

        or

            concat(s1, s2)

e.g.) program concatexample;
       var string1, string2, string3, string4 :
                                                varying [20] of char;
        begin
          string1 := 'abc';
          string2 := '123';
          string3 := string1 + string2;
          string4 := string2 + string1;
          writeln(string3);
          writeln(string4)
        end.

 will have the following output (try it!):

        abc123
        123abc

More precisely, here's what happens when this
program is executed:

1.  4 memory locations are reserved for 20-character
    strings.
2.  'abc' is stored in the mem. loc. named "string1"
3.  '123' is stored in the mem. loc. named "string2"
4.  the contents of string1 are concatenated with
    the contents of string2, and the result is stored
    in the mem. loc. named "string3"
5.  the contents of string2 are concatenated with
    the contents of string1, and the result is stored
    in the mem. loc. named "string4"
6.  the contents of string3 are printed out
7.    the contents of string4 are printed out

Make sure that you understand exactly how and why
this program behaves in this way.

If you have any questions, ask us in lab or in lecture,
or come see us during office hours!


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes18.23oc00.html