CMSC 331 Final Exam Fall 2013

Similar documents
UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam

Lisp. Versions of LISP

Functional Programming. Big Picture. Design of Programming Languages

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Functional Programming. Pure Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Programming Perls* Objective: To introduce students to the perl language.

PERL Scripting - Course Contents

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

Intro. Scheme Basics. scm> 5 5. scm>

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Regular expressions and case insensitivity

1. Introduction. 2. Scalar Data

Pathologically Eclectic Rubbish Lister

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Imperative, OO and Functional Languages A C program is

Functional Languages. Hwansoo Han

COMS 3101 Programming Languages: Perl. Lecture 2

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Chapter 15. Functional Programming Languages

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Scheme as implemented by Racket

Lecture #2 Kenneth W. Flynn RPI CS

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CPS 506 Comparative Programming Languages. Programming Language Paradigm

COP4020 Programming Assignment 1 - Spring 2011

CS 314 Principles of Programming Languages. Lecture 16

Introduction to Scheme

Regular expressions and case insensitivity

CSCI337 Organisation of Programming Languages LISP

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

A Quick Introduction to Common Lisp

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Strings Scheme: I/O

(Refer Slide Time: 01:12)

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Project 2: Scheme Interpreter

Perl. Many of these conflict with design principles of languages for teaching.

CS 314 Principles of Programming Languages

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

LECTURE 16. Functional Programming

CS 415 Midterm Exam Spring 2002

Lisp Basic Example Test Questions

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Functional Programming. Pure Functional Languages

Perl. Interview Questions and Answers

Functional Programming. Pure Functional Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

MIDTERM EXAMINATION - CS130 - Spring 2005

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Imperative languages

Lecture Notes on Lisp A Brief Introduction

Organization of Programming Languages CS3200/5200N. Lecture 11

Programming Languages

Sample Final Exam Questions

Functional Programming Languages (FPL)

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Perl. Perl. Perl. Which Perl

Chapter 11 :: Functional Languages

CS 11 Haskell track: lecture 1

A Brief Introduction to Scheme (II)

What is PERL?

Functional Programming Lecture 1: Introduction

A Small Interpreted Language

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

Lecture 2: Programming in Perl: Introduction 1

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

4/19/2018. Chapter 11 :: Functional Languages

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Symbolic Computation and Common Lisp

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.


Documentation for LISP in BASIC

Common Lisp. Blake McBride

CMSC 331 Final Exam Section 0201 December 18, 2000

Modularity and Reusability I. Functions and code reuse

T-( )-MALV, Natural Language Processing The programming language Perl

Example Scheme Function: equal

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Func%onal Programming in Scheme and Lisp

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Scheme: Expressions & Procedures

CSE 303 Midterm Exam

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

Functional programming with Common Lisp

Func%onal Programming in Scheme and Lisp

An Explicit-Continuation Metacircular Evaluator

Turtles All The Way Down

Structure and Interpretation of Computer Programs

Transcription:

CMSC 331 Final Exam Fall 2013 Name: UMBC username: You have two hours to complete this closed book exam. Use the backs of these pages if you need more room for your answers. Describe any assumptions you make in solving a problem. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy. Skim through the entire exam before beginning to get a sense of where best to spend your time. If you get stuck on one question, go on to another and return to the difficult question later. Comments are not required for programming questions but adding some might help us understand your code. 1. True/False (30 points) For each of the following questions, enter T (true) or F (false). (2 points each) 1. In Perl, a function takes a fixed number of arguments, set at compile-time. 2. Haskell s method of strong type checking means that type-mismatch errors are generally detected at compile-time, avoiding certain kinds of errors. 3. The assert statement in C and C++ is useful for an informal sort of program verification. 4. Modern functional languages like Haskell, which feature lazy evaluation, have performance advantages compared to older languages like Lisp. 5. In Haskell and Scheme, the read-eval-print loop is what reads commands from the user, performs them, and show the user the results. 6. If a function in Haskell has only one formal parameter, then it s a recursive function. 7. When Haskell complains about type mismatch, the problem could be as simple as misspelling the name of a function. 8. Currying refers to taking the results of one function and using it as the input to another function. 9. In Perl, wrapping a regular expression in parentheses is used to remember the matched string for use, for example, in an assignment statement. 10. If a finite-state machine has even only one state in which two outbound arcs have the same symbol attached, then the machine is non-deterministic. 11. Perl and Haskell have at least one feature in common: there is usually more than one way to do something. 12. One of the advantages of functional programming languages like Haskell is that once the program compiles, it is more likely to run correctly, i.e. without logic errors. 13. The C++ language allows for explicit definition of constructor and destructor functions. 14. Using lambda expressions, in a language like Haskell, one can define functions and use them without giving them a specific name. 15. A list comprehension is one way that Haskell provides for data structures that are essentially of infinite length. Page 1 of 9

2. Fill in the Blanks (20 points, 2 points each) 1. In UNIX, the command is used to make a file executable. 2. Recursive descent is an example of what is known as parsing. 3. In arrays, the subscripts may be integers but other types may be used as well. 4. In Perl, it is sometimes useful to understand whether a variable is being used in the list or context. 5. Alan Turing s work on early computers played an important role in the Battle of the Atlantic during World War. 6. In Haskell, the class restriction requires that the input argument(s) have a defined less than (<) function. (Capitalization counts on this problem.) 7. In Python, a copy operation is said to be if it uses pointers, rather than allocating new storage, as it performs the copying. 8. In some languages including Haskell, functions are functions that take other functions as arguments. 9. When writing functions in Haskell, it s important to name functions so that they don t conflict with functions already defined in the. 10. In several languages, a expression is used to define a function that might otherwise not have a name. Page 2 of 9

3. Understanding Lisp and Scheme (10 points) Consider the following Scheme program, which compiles and runs to completion on GL. #lang scheme ; scheme problem for final exam (define (afunc lis) (cond ((null? lis) 0) ((not (list? (car lis))) (cond ((null? (car lis)) (afunc (cdr lis))) (else (+ 1 (afunc (cdr lis)))))) (else (+ (afunc (car lis)) (afunc (cdr lis)))))) (when #t (printf "Hello, world!~n") (printf "~a~n" (afunc '())) (printf "~a~n" (afunc '((1 2) (3 4 5)))) ) (5 points) What does this program do? (5 points) What is the output of this program? Page 3 of 9

4. Understanding Perl (10 points) Consider a function maxint that takes a list of positive integers and returns 0 if the list is empty and the largest value in the list if it is not empty. The following is a recursive version of the maxint function in Perl. Fill in the blanks. #!/usr/bin/perl print "Recursive version of maxint function!\n"; # a short Perl example use strict; # an example of a pragma use warnings; { if ( == 0) { # check for empty list 0; else { $first = ; # save first element of input list my @newlist = @_; # make a copy of the input list @newlist; # remove its first element my $max1 = maxint(@newlist); # recursion if ( > ) { else { my @alist = (7,3,4,5); my $max1 = maxint(@alist); print "maxint of @alist is $max1\n"; Page 4 of 9

5. Functional programming (20 points) Once again, consider a function maxint that takes a list of positive integers and returns 0 if the list is empty and the largest value in the list if it is not empty. Examples of this function s behavior are shown in the box to the right. > maxint([]) 0 > maxint([3]) 3 > maxint([3, 2, 4, 9, 1]) 9 (a) (10 points) Write a recursive version of this function in YOUR CHOICE of either Lisp (Scheme) or Haskell. (b) (10 points) Given a list L and the function maxint as described above, write a function AllBut- Max that returns a list consisting of only those elements of L that are less than the max. There may be several ways to do this, but make sure the function maxint is called only once. YOUR CHOICE of Lisp or Haskell. Page 5 of 9

6. Programming in Haskell (10 points) Consider the function productseries, defined as: productseries(k) = (1/1) * (1/2) * (1/3) * * (1/k) for any positive integer k. Implement this function in Haskell (not LISP) using some appropriate combination of recursion or high order functions. Again, there may be several ways to do this. Page 6 of 9

LISP Cheat Sheet condensed version (defun helloworld () ;; prints the standard message (print "Hello World")) (defun predicatedemo () (print (member 3 (list 1 2 3 4 5 6))) ; prints (3 4 5 6) (print (symbolp 'foo)) ; should be TRUE (print (eq 4 (+ 2 2))) ; good for atoms (print (equal '(1 2 3) '(1 2 3))) ; good for other things (print (null ())) ; should be TRUE (print (listp '(1 2 3))) ; should be TRUE (print (listp '3)) ; should be FALSE ) ;; basic lisp manipulation (defun listdemo() (print (cons 'a 'b)) ; creates a dotted pair (car '(this is a list)) (cdr '(this is another list)) (cons 'quick '(brown fox)) (append '(1 2) '(3 4)) (print (reverse '(1 2 3))) ) ;; every language needs an if statement (print (if (= (+ 1 2) 3) 'yup 'nope)) ;; the cond statement does more! (defun conddemo() (let ((a 2)) (cond ((eq a 1) (princ "a is 1")) ((eq a 3) (princ "a is 3")) (t (princ "a is something else")))) ;; example of mapcar (mapcar #'sqrt '(1 2 3 4 5)) (mapcar (function sqrt) '(1 2 3 4 5)) ; both return (1 1.4142135 1.7320508 2 2.236068) ;; example of apply (apply #'append '((10 20) (30) (40 50))) ; returns (10 20 30 40 50) Page 7 of 9

Perl Cheat Sheet condensed version #!/usr/bin/perl print "Hello, world!\n"; # a short Perl example # Perl statements end with a semicolon # ordinary variables begin with $ my $ndocs = 0; # perl is great for working with strings # strings are delimited with either single quotes or double quotes my $bstring = "Inside double quotes, usual escapes apply"; # period. is the string concatenation operator my $alongstring = $astring."\nconcatenated with the period operator\n". "and the x factor for repetition\n$bstring\nand interpolation!"; # LOTS of pattern matching operators, including s for substitute $astring = "Do not do that!\n"; print $astring; $astring =~ s/do not/don't/; print $astring; # nothing special about Boolean variables # if it's zero, it's false, otherwise it's true my $aboolean = $fred lt $barney; # control structures include if # although we just defined $aboolean, we can always test if (defined($aboolean) && $aboolean) { print "fred is less than barney\n"; else { print "fred is NOT less than barney\n"; # Perl supports lists and arrays, closely related concepts # subscripts start at zero normally, as in C my @fred; $fred[0] = 2.8; $fred[1] = "Wilma!"; $fred[2] = 'dino' x 2; # push and pop add or delete elements from the end of a list # use $fs to access an individual element of the list # but we can use @fs to refer to the whole list # # sometimes we consider what Perl expects, i.e. list vs. scalar context # my @fs = qw/fred wilma barney betty/; push @fs, qw/bambam pebbles/; # shift and unshift delete or add elements to the start of a list unshift @fs, "dino"; # list elements are separated by blanks when interpolated print "@fs\n"; my @sf = reverse(@fs); printf "Print the list in reverse @sf\n"; # simple echo of a given input file (default to STDIN) to STDOUT sub echodemo { # note use of @_ in both the scalar and list contexts in this if my $XLSfile = "default.xls"; if (@_ == 1) { # list of arguments in list context Page 8 of 9

($XLSfile) = @_; else { print "usage: echodemo(inputfile=stdin[,xlsfile=default.xls]\n"; my $word; my $nterms=0; # Perl has built-in hash functions my %ahashtable = (); # will be used in example below while (my $inputline = <STDIN>) { chomp $inputline; # get rid of trailing newline print STDOUT "$inputline\n"; # I/O looks very C-like, eh? # recall that array names begin with @ # split $inputline into an array of blank-separated words my @words=split(" ", $inputline); foreach $word (@words) { $ahashtable{$word += 1; # to practice with regular expression matching, see if word # would be a good password, i.e. having at least one # digit, one lower case letter, and one upper case letter # make sure it finds a good password when run on itself XYzzy18 if ($word =~ /[A-Z]/ && $word =~ /[a-z]/ && $word =~ /[0-9]/) { print "$word would be a good password.\n"; for $word (sort(keys(%ahashtable))) { $nterms++; # but let's make each word lower-case # and make a variable wordlc local to this block my $wordlc = $word; $wordlc =~ tr/a-z/a-z/; # tr stands for translate printf STDOUT "%s, %d\n", $wordlc, $ahashtable{$word; # # working with files and directories, and patterns my @files = <*>; foreach my $myfile (@files) { if (-f $myfile and -r $myfile) { # see if it's a readable file my ($nl, $nw, $nch) = `wc $myfile` =~ /([0-9]+)\s+([0-9]+)\s+([0-9]+)/; print "file $myfile has $nl lines, $nw words and $nch characters\n"; Page 9 of 9