2002-Programming_Languages-scanned-solutions

2002-Programming_Languages-scanned-solutions -...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Comprehensive Exam: Programming Languages Autumn 2002 1. (7 points) Type Inference (a) (3 points) This part of the question asks you to write the ML type of an ML function. Recall that an ML function that takes two integer arguments and returns a Boolean value has type (int * int) -+ bool. What is the ML type for the following function? fun applytwice(f , x) = f (f (x)) ; Answer: ('a -> 'a) * 'a -> 'a (b) (2 points) Describe types <typel>, <type2> and <type3> that will make the analo- gous C code below statically typed. You may write the types using correct or mostly correct C syntax. In particular you can write <type2> f in a form where f appears in the middle of the type, if you prefer (and if this is correct). <typel> applytwice(<type2> f, <type3> x) ( return f (f (x) ) ; J; Answer: One possibility is <typeI>=<type3>=int and <type2>= int (*) (int). Some prefer to write int (* f) (int) (c) (2 points) Why is the ML type more useful than the C type for applytwice? Specif- ically, what flexibility does the ML type system give, for functions with the kind of type you wrote in (a), that the C type system does not give you for the function in (b) ? Answer: The ML function is polymorphic, which means that you can apply applytwice to many types of arguments. In contrast, the C function can only be declared and used for one specific choice of types. 2. (5 points) ALGOL 60 Pass-By-Name In pass-by-name, the result of a procedure call is the same as if the formal parameters
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/18/2011 for the course CS 242 at Stanford.

Page1 / 4

2002-Programming_Languages-scanned-solutions -...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online