Scripting Languages CS5142, Fall 2013 hw04


Reading Assignments

Concept Questions

hw04_1 Weak, strong, static, and dynamic typing

1a.
(3 points) Compare Perl and VBA in terms of the strength of their type system. Which one is more strongly typed, and why?
1b.
(3 points) Compare Perl and VBA in terms of the dynamicity of their type system. Which one is more dynamically typed, and why?
1c.
(4 points) Given an equivalent level of dynamicity of the type system, what advantage might a more weakly typed language have over a more strongly typed language?

hw04_2 Context in Perl

Consider the following Perl script:
#!/usr/bin/env perl
use strict;
use warnings;
sub f1($) { my ($v) = @_; print "f1 $v\n"; }
sub f2(@) { my ($v) = @_; print "f2 $v\n"; }
my $s = "ww";
my @a = ("xx", "yy", "zz");
f1 $s; f1 @a; f2 $s; f2 @a;
2a.
(2 points) What does it print?
2b.
(4 points) Explain the behavior.
2c.
(4 points) Why is it useful to support this behavior in a programming language?

Programming exercises

hw04_3 Priority queue class

(10 points) Write a Perl class that implements a priority queue of numbers. Your class should have two methods: insert(key) and extractMax(). Your implementation should be robust with respect to queue size, growing the underlying data structures if necessary. Your code does not need to be efficient, so you do not need to find the fastest possible algorithm to solve this. Consider the following driver script:
#!/usr/bin/env perl
use warnings;
use strict;
use PriorityQueue;
my $q = new PriorityQueue();
$q->insert(3.1);
$q->insert(4.2);
$q->insert(0.5);
$q->insert(1.9);
print $q->extractMax() . "\n";
print $q->extractMax() . "\n";
print $q->extractMax() . "\n";
print $q->extractMax() . "\n";
This should print 4.2, 3.1, 1.9, and 0.5, in other words, it should output the previously-inserted numbers in descending order by value. To solve this question, you need to put your code in a file PriorityQueue.pm, so that the driver can pick it up via the use PriorityQueue statement.

hw04_4 Gunning fog index

(10 points) Quoting wikipedia, “the Gunning fog index measures the readability of English writing”. Write a Perl script that computes the Gunning fog index for an input text. You should use the following algorithm: For example, given the following input text from wikipedia:
The fog index is commonly used to confirm that text can be read
easily by the intended audience. Texts for a wide audience generally
need a fog index less than 12. Texts requiring near-universal
understanding generally need an index less than 8.
This text has 3 sentences and 41 words, including the 6 hard words audience, audience, general, universal, understand, and general. There are around 13.7 words per sentence with 14.6% hard words. The resulting fog index is therefore around 11.3.
http://www.cs.cornell.edu/Courses/cs5142/2013fa/hw04.html