Docsity

Programming Languages - Homework 7 Questions | CS 3723, Assignments of Programming Languages

Toggle sidebar
bg1
CS3723 Homework 7
due Nov 21, 2008 (electronic submission)
The concept of abstraction (information hiding) can be supported both through scoping
rules and through type checking. For example, the following ML code defines an abstraction
through scoping rules and higher-order functions.
fun mk_exam(passwd : string) =
let val p_problems = ref nil;
val p_solutions = ref nil;
fun add_problem(question:string, answer: string) =
(p_problems := question :: !p_problems;
p_solutions := answer :: !p_solutions);
fun get_problems() = !p_problems;
fun get_solutions(pass) = if (pass = passwd) then !p_solutions else nil
in { add=add_problem, problems = get_problems, solutions = get_solutions }
end;
val midterm = mk_exam("17db");
#add(midterm)("problem1", "solution1");
#problems(midterm)();
Run the above ML code and check out what is the returned type for midterm and what
is the last returned value.
1. The above ML code is similar to the following abstype definition
abstype Exam = Exam of string list ref * string list ref * string with
fun mk_exam(passwd) = Exam(ref nil, ref nil, passwd)
fun add_problem(Exam(p_problems,p_solutions,passwd), question, answer) =
(p_problems := question :: !p_problems;
p_solutions := answer :: !p_solutions)
fun get_problems(Exam(p_problems,p_solutions,passwd)) = !p_problems
fun get_solutions(Exam(p_problem,p_solutions,passwd), pass) =
if (pass = passwd) then !p_solutions else nil
end;
Translate the above abstraction into an object-oriented class in C++.
2. What is fundamentally different between the higher-order function implementation of
the abstraction and the ML abstype implementation? What is fundamentally different
between the ML abstype implementation and your C++ implementation? Provide
your answer as comment (i.e., enclosing it inside a pair of โ€œ/*โ€ and โ€œ*/โ€) and attach
the answer at the end of your C++ code.
1

Partial preview of the text

Download Programming Languages - Homework 7 Questions | CS 3723 and more Assignments Programming Languages in PDF only on Docsity!

CS3723 Homework 7

due Nov 21, 2008 (electronic submission)

The concept of abstraction (information hiding) can be supported both through scoping rules and through type checking. For example, the following ML code defines an abstraction through scoping rules and higher-order functions.

fun mk_exam(passwd : string) = let val p_problems = ref nil; val p_solutions = ref nil; fun add_problem(question:string, answer: string) = (p_problems := question :: !p_problems; p_solutions := answer :: !p_solutions); fun get_problems() = !p_problems; fun get_solutions(pass) = if (pass = passwd) then !p_solutions else nil in { add=add_problem, problems = get_problems, solutions = get_solutions } end; val midterm = mk_exam("17db"); #add(midterm)("problem1", "solution1"); #problems(midterm)();

Run the above ML code and check out what is the returned type for midterm and what is the last returned value.

  1. The above ML code is similar to the following abstype definition

abstype Exam = Exam of string list ref * string list ref * string with fun mk_exam(passwd) = Exam(ref nil, ref nil, passwd) fun add_problem(Exam(p_problems,p_solutions,passwd), question, answer) = (p_problems := question :: !p_problems; p_solutions := answer :: !p_solutions) fun get_problems(Exam(p_problems,p_solutions,passwd)) = !p_problems fun get_solutions(Exam(p_problem,p_solutions,passwd), pass) = if (pass = passwd) then !p_solutions else nil end;

Translate the above abstraction into an object-oriented class in C++.

  1. What is fundamentally different between the higher-order function implementation of the abstraction and the ML abstype implementation? What is fundamentally different between the ML abstype implementation and your C++ implementation? Provide your answer as comment (i.e., enclosing it inside a pair of โ€œ/โ€ and โ€œ/โ€) and attach the answer at the end of your C++ code.