Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Document preview thumbnail
Vista previa 1 fuera de 2 páginas
Examen

Department of Computer ScienceMidterm Examination 3 Computer Science 312 Functional and Logic Programming

Document preview thumbnail
Vista previa 1 fuera de 2 páginas

Department of Computer ScienceMidterm Examination 3 Computer Science 312 Functional and Logic Programming Question 1 [12 marks] Consider the program zipWith _ _ [] = [] zipWith _ [] _ = [] zipWith f (h1:t1) (h2:t2) = f h1 h2 : zipWith f t1 t2 (a) [4 marks] What is the inferred type of zipWith? (b) [4 marks] What is the value of zipWith (*) [1,2,3,4] [5,4,3,2,1] [You do not need to show your reasoning if you get the correct answer, but if you want partial marks, you need to explain your answer.] (c) [4 marks] The standard zip function has the following behaviour:: zip [1,2,3,4] [11,22,33,44] evaluates to [(1,11),(2,22),(3,33),(4,44)] zip [1,2,3,4] "abcdef" evaluates to [(1,’a’),(2,’b’),(3,’c’),(4,’d’)] Define zip using zipWith. You may use lambda ( ), and tuples, but no other function. This should not be a recursive definition. Question 2 [10 marks] Consider the function mapWhile f p xs such that mapWhile f p [x1, x2, . . . , xn] = [f x1, f x2, . . . , f xk] where k is the largest index such that p xi is true for all i k. It should have the following behaviour: *Main mapWhile [9,8,7,6,5] (2+) (2) [7,6,5,4,3,2,1,21,20,19] *Main mapWhile [] (2+) (2) [1,7,6,5,4,3,2,1,21,20,19] *Main mapWhile (2+) (0) [1,7,6,5,4,3,2,1,21,20,19] [3,9,8,7,6,5,4,3,23,22,21] *Main mapWhile (ˆ2) (x - xˆ2 60) [1..] [1,4,9,16,25,36,49] (a) [4 marks] The type of mapWhile is: mapWhile :: (t - t1) - (t - Bool) - [t] - [t1] Explain in English (suitable for a CPSC 312 student who has just been introduced to Haskell) what this means. (b) [6 marks] Fill in the missing values in the following recursive definition of mapWhile. This should use pattern matching as much as possible. You can use : and [ ], but no other Haskell functions. mapWhile f p = mapWhile f p (x:xs) | = : | otherwise = Question 3 [10 marks] In this question you can use: list comprehensions [ f x | x - list, cond x ] foldr ⊕ v [a1, a2, ..an] = a1 ⊕ (a2 ⊕ (... ⊕ (an ⊕ v))) foldl ⊕ v [a1, a2, ..an] = (((v ⊕ a1) ⊕ a2) ⊕ ...) ⊕ an (a) [6 marks] Implement zipWith (as defined earlier) in terms of zip (defined earlier) and either list com- prehensions, foldr or foldl. You may use lambda () and : but no other built-in functions. (b) [4 marks] Given the definition: delal p lst = foldr (x y - if p x then y else x:y) [] lst vowel x = x ‘elem‘ "aeiou" What is the value of delal vowel "abcef" [You do not need to show your reasoning if you get the correct answer, but if you want partial marks, you need to explain your answer.] Question 4 [10 marks] (a) [3 marks] Why is lazy evaluation preferable to call-by-name? (There is no need to define call-by- name.) (b) [4 marks] Explain the meaning of the type declaration: sum :: Num a = [a] - a (c) [3 marks] In Haskell every function takes a single argument. What does this mean for functions that take 2 arguments, such as (+)?


Información del documento

Subido en
5 de diciembre de 2022
Número de páginas
2
Escrito en
2022/2023
Tipo
Examen
Contiene
Preguntas y respuestas
$7.99

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
SmartMind
3.5
(22)
Vendido
110
Seguidores
112
Artículos
1680
Última venta
1 año hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes

Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.