CMPSC 461:编程语言概念
期中2练习题
球拍编程
问题1
a)(2分)给定以下球拍代码,程序的输出是什么?
#lang球拍
(定义(mystery1 t)(folds cons null t))
(mystery1’(1 2 3))
b)(6分)我们定义值的大小如下:非列表值的大小为1;非列表值的大小为1。列表值的大小是
其元素大小的总和;空列表的大小为0。实现递归函数sizeOf
它接受一个值并返回该值的大小。例如:
(sizeOf 2);返回1
(sizeOf’(1(“ abc”())2)));返回3
(sizeOf’());返回0
(sizeOf’(a b(c d e(f g))((h i)(j))));返回10
使用(list?v)检查值v是否为列表。
c)(4pt)编写一个函数平方表,该函数给定一个数字列表,创建一个相同平方的列表
数字。您不能使用RECURSION来实现此功能。使用地图。您的职能
应该执行以下操作:
(方列表’());返回’()
(方列表’(1 2 3 4));传回’(1 4 9 16)
Lambda微积分
问题2对于以下lambda项:
T?(λxy。x y)(λx。y)x
a)(4点)计算自由变量FV(T)的集合,并将T中的所有绑定变量连接到它们的定义
与线。例如,λx的绑定变量。 x y应该是λx。 X Y。
b)(8分)完全评估T,这样就不可能进一步减少β了。显示计算的详细步骤。
问题3回顾我们在课堂上定义的教堂编码的以下定义,
n?λfz。 n
零?λfz。 ?
一个?λfz。 z
SUCC?λn。 λfz。 f(n f z)
加?λmn。 m SUCC n
对?λxy f.f x y
左?λp.pλxy.x
右?λp.pλxy.y
1/3
a)完全评估RIGHT(对1 0),以致无法进一步减少β。显示您的详细步骤
计算。
b)在λ微积分中定义一个函数MULTPAIR,以便在给定两个教堂数字对的情况下,返回一个
代表两个数字乘积的教堂数字。例如,MULTPAIR(对2 3)
应该评估为6(或在α/β减少下的某个等效项)。您可以使用上面所有的λ项。
子例程和控件抽象
问题4考虑以下伪代码。
int a = 0;
无效A(int m){
打印
m = a;
}
void main(){
int a = 1,b = 2;
A(b);
打印b;
}
1.(4pt)如果语言使用静态作用域并且所有参数均按值传递,那么输出将是什么?
2.(4pt)如果语言使用动态作用域并且所有参数都通过传递,输出将是什么
参考?
3.(4分)如果语言使用静态作用域并且所有参数均通过值传递,则输出是什么?
结果?
问题5考虑下面的Racket代码,该代码计算从0到n的平方和。
(定义(和平方n)(如果(= n 0)0(+(和平方(-n 1))(* n n))))
1.(4pt)解释为什么sumsquare的此实现将花费很长时间运行并使用大量
n大的内存(有时甚至耗尽内存)。
2.(4pt)重写此函数sumsquare,以使其运行更快且不会耗尽
n较大的内存。
问题6(4pt)异常处理使用以下Java程序的输出是什么
“替换”语义?
导入java.io. *;
p2类{
私人静态void foo(Integer i){
尝试 {
System.out.println(“ A”);
中期2练习题,Cmpsc 461
2021年春季
int j = i.intValue();
System.out.println(“ B”);
j = j / 0;
System.out.println(“ C”);
}
catch(ArithmeticException e){
System.out.println(“ D”);
}
catch(NullPointerException e){
System.out.println(“ E”);
}
最后 {
System.out.println(“ F”);
}CMPSC 461 编程代写,代做java
}
公共静态void main(String [] args){
尝试 {
System.out.println(“ G”);
foo(null);
整数x = 461/0;
System.out.println(“ H??”);
}
catch(ArithmeticException e){
System.out.println(“ I”);
}
catch(NullPointerException e){
System.out.println(“ J”);
}
最后 {
System.out.println(“ K”);
}
}
}
中期2练习题,Cmpsc 461
CMPSC 461: Programming Language Concepts
Midterm 2 Practice Questions
Racket Programming
Problem 1
a) (2pt) Given the following racket code,what is the output of the program?
#lang racket
(define (mystery1 t) (foldl cons null t))
(mystery1 ’(1 2 3))
b) (6pt) We define the size of a value as follows: the size of a non-list value is 1; the size of a list value is the
sum of the sizes of its elements; the size of an empty list is 0. Implement a recursive function sizeOf
that takes a value and returns the size of that value. For example:
(sizeOf 2) ; returns 1
(sizeOf ’(1 ("abc" ()) 2)) ; returns 3
(sizeOf ’()) ; returns 0
(sizeOf ’(a b (c d e (f g)) ((h i) (j)))) ; returns 10
Use (list? v) to check if a value v is a list.
c) (4pt) Write a function squarelist that given a list of numbers, create a list of the square of the same
numbers. You CAN NOT USE RECURSION to implement this function. Use map. Your function
should do the following:
(squarelist ’()) ; returns ’()
(squarelist ’(1 2 3 4)) ; returns ’(1 4 9 16)
Lambda Calculus
Problem 2 For the following lambda term:
T ? (λx y. x y) (λx. y) x
a) (4pt) Calculate the set of free variables FV(T) and connect all bound variables in T to their definitions
with lines. For example, the bound variables for λx. x y should be λ x. x y.
b) (8pt) Fully evaluate T so that no further β-reduction is possible. Show detailed steps of your computation.
Problem 3 Recall the following definitions of Church Encoding we defined in our class,
n ? λf z. fn z
ZERO ? λf z. z
ONE ? λf z. f z
SUCC ? λn. λf z. f (n f z)
PLUS ? λm n. m SUCC n
PAIR ? λx y f.f x y
LEFT ? λp.p λx y.x
RIGHT ? λp.p λx y.y
1/3
a) Fully evaluate RIGHT (PAIR 1 0) so that no further β-reduction is possible. Show detailed steps of your
computation.
b) Define a function MULTPAIR in λ-calculus so that given a pair of two church numerals, it returns a
church numeral that represents the product of the two numbers. For example, MULTPAIR (PAIR 2 3)
should evaluate to 6 (or some equivalent term under α/β-reduction). You can use all λ-terms above.
Subroutine and Control Abstraction
Problem 4 Consider the following pseudo code.
int a=0;
void A(int m) {
print a;
m = a;
}
void main () {
int a=1, b=2;
A(b);
print b;
}
1. (4pt) What are the outputs if the language uses static scoping and all parameters are passed by value?
2. (4pt) What are the outputs if the language uses dynamic scoping and all parameters are passed by
reference?
3. (4pt) What are the outputs if the language uses static scoping and all parameters are passed by value-
result?
Problem 5 Consider the following Racket code that calculates sum of squares from 0 to n.
(define (sumsquare n) (if (= n 0) 0 (+ (sumsquare (- n 1)) (* n n))))
1. (4pt) Explain Why this implementation of sumsquare will take a long time to run and uses a lot of
memory (sometimes even run out of memory) with a large n.
2. (4pt) Rewrite implement this function sumsquare so that it will run faster and doesn’t run out of
memory even with a large n.
Problem 6 (4pt) What is the output of the following Java program where the exception handling uses
”replacement” semantics?
import java.io.*;
class p2 {
private static void foo(Integer i) {
try {
System.out.println("A");
Midterm 2 Practice Questions, Cmpsc 461
2021 Spring
int j = i.intValue();
System.out.println("B");
j = j / 0;
System.out.println("C");
}
catch (ArithmeticException e) {
System.out.println("D");
}
catch (NullPointerException e) {
System.out.println("E");
}
finally {
System.out.println("F");
}
}
public static void main(String[] args) {
try {
System.out.println("G");
foo(null);
int x = 461/0;
System.out.println("H");
}
catch (ArithmeticException e) {
System.out.println("I");
}
catch (NullPointerException e) {
System.out.println("J");
}
finally {
System.out.println("K");
}
}
}
Midterm 2 Practice Questions, Cmpsc 461
请加QQ:99515681 或邮箱:99515681@qq.com WX:codehelp
CMPSC 461: Programming Language
原文:https://www.cnblogs.com/nicajava/p/14619145.html