From last week. Lecture 5. Outline. Principles of programming languages

Similar documents
A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Last class. n Scheme. n Equality testing. n eq? vs. equal? n Higher-order functions. n map, foldr, foldl. n Tail recursion

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CS 11 C track: lecture 1

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

implement language system

n Haskell n Syntax n Lazy evaluation n Static typing and type inference n Algebraic data types n Pattern matching n Type classes

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Computers and Scientific Thinking

. Written in factored form it is easy to see that the roots are 2, 2, i,

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Last Class. Announcements. Lecture Outline. Types. Structural Equivalence. Type Equivalence. Read: Scott, Chapters 7 and 8. T2 y; x = y; n Types

n The C++ template facility provides the ability to define n A generic facility allows code to be written once then

Chapter 2. C++ Basics. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

int f(char a, char b) {

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Python Programming: An Introduction to Computer Science

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

CMPT 125 Assignment 2 Solutions

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Python Programming: An Introduction to Computer Science

Lecture 9: Exam I Review

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Today s objectives. CSE401: Introduction to Compiler Construction. What is a compiler? Administrative Details. Why study compilers?

top() Applications of Stacks

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Lecture 1. Topics. Principles of programming languages (2007) Lecture 1. What makes programming languages such an interesting subject?

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CS : Programming for Non-Majors, Summer 2007 Programming Project #3: Two Little Calculations Due by 12:00pm (noon) Wednesday June

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

BOOLEAN MATHEMATICS: GENERAL THEORY

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Module 8-7: Pascal s Triangle and the Binomial Theorem

n Haskell n Covered syntax, lazy evaluation, static typing n Algebraic data types and pattern matching n Type classes n Monads and more n Types

Elementary Educational Computer

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Analysis of Algorithms

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

BaanERP Tools. Programming features

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

Java Expressions & Flow Control

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Designing a learning system

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

Appendix D. Controller Implementation

Data Structures and Algorithms. Analysis of Algorithms

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Designing a learning system

Project 2.5 Improved Euler Implementation

Code Review Defects. Authors: Mika V. Mäntylä and Casper Lassenius Original version: 4 Sep, 2007 Made available online: 24 April, 2013

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

n We have discussed classes in previous lectures n Here, we discuss design of classes n Library design considerations

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Solutions to Final COMS W4115 Programming Languages and Translators Monday, May 4, :10-5:25pm, 309 Havemeyer

The isoperimetric problem on the hypercube

Morgan Kaufmann Publishers 26 February, COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 5

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

10/23/18. File class in Java. Scanner reminder. Files. Opening a file for reading. Scanner reminder. File Input and Output

Ones Assignment Method for Solving Traveling Salesman Problem

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

5.3 Recursive definitions and structural induction

CS 111: Program Design I Lecture 15: Modules, Pandas again. Robert H. Sloan & Richard Warner University of Illinois at Chicago March 8, 2018

The Magma Database file formats

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

CS 111: Program Design I Lecture 21: Network Analysis. Robert H. Sloan & Richard Warner University of Illinois at Chicago April 10, 2018

CMSC Computer Architecture Lecture 3: ISA and Introduction to Microarchitecture. Prof. Yanjing Li University of Chicago

condition w i B i S maximum u i

COP4020 Programming Languages. Compilers and Interpreters Prof. Robert van Engelen

CS 111: Program Design I Lecture 15: Objects, Pandas, Modules. Robert H. Sloan & Richard Warner University of Illinois at Chicago October 13, 2016

Arithmetic Sequences

Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18

Location Steps and Paths

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows:

On Infinite Groups that are Isomorphic to its Proper Infinite Subgroup. Jaymar Talledo Balihon. Abstract

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Package RcppRoll. December 22, 2014

How do we evaluate algorithms?

Data diverse software fault tolerance techniques

Data Structures Week #5. Trees (Ağaçlar)

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

One advantage that SONAR has over any other music-sequencing product I ve worked

CSE 417: Algorithms and Computational Complexity

Transcription:

Priciples of programmig laguages From last week Lecture 5 http://few.vu.l/~silvis/ppl/2007 Natalia Silvis-Cividjia e-mail: silvis@few.vu.l ML has o assigmet. Explai how to access a old bidig? Is & for logical ad? If both operads are boolea, the & ca be used as && but without short-circuitig ML: Value of a i halve? vrije Uiversiteit amsterdam Outlie Part I. Polymorphism Part II. Fuctioal programmig. Third look at ML. 1

What is polymorphism? Polymorphism poly+morphos (Greek) = may forms (shapes) Used i may other scieces: material sciece, biology I programmig laguages PMF is difficult to defie: Applies to a wide variety of laguage features Most laguages have at least a little First we examie a few major examples, the we try to give a defiitio that covers them Polymorfism examples Overloadig Parameter coercio Parametric polymorphism Defiitios ad classificatios What is overloadig? Overloadig = to give more tha oe defiitio, all of differet types You ca overload: operators or fuctio ames Predefied Overloaded Operators Some operators are already overloaded by the laguage itself. ML: val x = 1 + 2; val y = 1.0 + 2.0; C++: a = 1 + 2; b = 1.0 + 2.0; c = "hello " + "there"; How does the laguages system kow which defiitio to use? 2

User-Overloaded Operators Situatios: You ca add two itegers or two floats, but what if you wat to add two complex umbers? You ca compare two itegers, two strigs, but how to compare 2 C structures? Ex: Overloadig the < operator i C++ #iclude <iostream> #iclude <strig> usig amespace std ; struct Studet { strig ame ; it stud_r ; it grade ; ; // overloads < operator to compare 2 structs of type Studet bool operator< (cost Studet& studet1, cost Studet& studet2) { if (studet1.grade < studet2.grade) retur true ; else retur false ; Ex: Overloadig the < operator i C++ (ctd) it mai() { Studet me, you ; me.ame = "Sheila" ; me.stud_r =14537780 ; me.grade = 8 ; you.ame = "Bob" ; you.stud_r =14532180 ; you.grade = 4 ; if (me < you) cout << you.ame << " is more clever tha " << me.ame ; else cout << me.ame << " is more clever tha " << you.ame ; cout << edl ; retur 0 ; Output: Sheila is more cleaver tha Bob C++ Operator overloadig rules All operators ca be overloaded except:. (direct member), :: (scope resolutio),.* ad?: You caot chage the uary/biary ature of a operator. You caot override precedece rules. Take care! Overloadig ca become cofusig. The user has to use his commo sese ad ot overdo it. Overloaded Fuctio Names I some laguages (Java, C++), the user ca overload the fuctio ame = same ame, differet defiitio (differet sematics) it square(it x) { retur x*x; double square(double x) { retur x*x; The bodies look the same, but the implemetatio by hardware is very differet. Implemetig Overloadig Compilers implemet overloadig (solve the ambiguity) like this: Create a set of moomorphic fuctios, oe for each defiitio Ivet a magled ame for each, ecodig the type iformatio Ex: fred_fii Have each referece use the appropriate magled ame, depedig o the parameter types 3

Mai feature of overloadig Polymorphism examples The laguage system looks at the operators type ad decides which defiitio to use. Overloadig Parameter coercio Parametric polymorphism Defiitios ad classificatios Coercio Defiig Coercios A coercio is a implicit type coversio, supplied automatically Explicit type coversio i Java: Implicit coversio (Coercio) i Java: OR double x; x = (double) 2; double x; x = 2; This coercio is ot polymorphism, x is ot polymorphic Laguage defiitios ofte take may pages to defie exactly which coercios are performed Some laguages, especially some older laguages like Algol 68 ad PL/I, have very extesive powers of coercio Some, like ML, have oe Most, like Java, are somewhere i the middle Defiig coercio i Java 5.6.1 Uary Numeric Promotio Some operators apply uary umeric promotio to a sigle operad, which must produce a value of a umeric type: If the operad is of compile-time type byte, short, or char, uary umeric promotio promotes it to a value of type it by a wideig coversio ( 5.1.2). Otherwise, a uary umeric operad remais as is ad is ot coverted. Uary umeric promotio is performed o expressios i the followig situatios: the dimesio expressio i array creatios ( 15.9); the idex expressio i array access expressios ( 15.12); operads of the uary operators plus + ( 15.14.3) ad mius - ( 15.14.4)... Parameter Coercio if a laguage supports coercio of parameters o a fuctio call (or of operads whe a operator is applied), the resultig fuctio (or operator) is polymorphic The Java Laguage Specificatio James Goslig, Bill Joy, Guy Steele 4

Example: Java void f(double x) { f((byte) 1); f((short) 2); f('a'); f(3); f(4l); f(5.6f); This f ca be called with ay type of parameter Java is willig to coerce to type double f is polymorphic Coercio vs. Overloadig There are potetially tricky iteractios betwee overloadig ad coercio Overloadig uses the types to choose the defiitio Coercio uses the defiitio to choose a type coversio Ambiguities might appear ad each laguage system has to solve them i some way. Ambiguity Example Ambiguity Example Suppose that, like C++, a laguage is willig to coerce char to it or to double it square(it x) { retur x*x; double square(double x) { retur x*x; Which square gets called for square('a')? Suppose that, like C++, a laguage is willig to coerce char to it Which f gets called for f('a', 'b')? void f(it x, char y) { void f(char x, it y) { Outlie Parametric Polymorphism Overloadig Parameter coercio Parametric polymorphism Defiitios ad classificatios A fuctio exhibits parametric polymorphism if it has a type that cotais oe or more type variables - fu f(a, b) = (a = b); val f = f : ''a * ''a -> bool A type with type variables is a polytype Foud i ML, C++ ad Ada, Java 5

Ex: C++ Fuctio templates // returs the maximum of 2 itegers it max (it left, it right) { if (left < right) retur right ; else retur left ; What is the problem here? What do we eed to solve it? //returs the maximum of 2 doubles double max (double left, double right) { if (left < right) retur right ; else retur left ; Ex: C++ Fuctio templates A fuctio template for the fuctio max: template <class T> T max (T left, T right) { if (left < right) retur right ; else retur left ; Here class T meas type T. T is a type variable T ca be ay type for which the operator < is defied For other types, operator < ca be overloaded. Ex: C++ Fuctio templates #iclude <iostream> usig amespace std;.. place here the fuctio template it mai() { it iteger1 = 4 ; it iteger2 = 10 ; it max1 = max (iteger1, iteger2); cout << "The maximum iteger is " << max1 << edl ; double double1 = 100.20 ; double double2 = 5.7 ; double max2 = max (double1,double2) ; cout << "The maximum double is " << max2 << edl ; retur 0 ; Implemetatio May copies vs. oe copy A improved implemetatio for parametric polymorphism is a active are of programmig laguage research Outlie Overloadig Parameter coercio Parametric polymorphism Defiitios ad classificatios So what is polymorphism? 6

A attempt at a defiitio A fuctio or operator is polymorphic if it has at least two possible types. How may types? ad hoc polymorphism if it has oly fiitely may possible types uiversal polymorphism if it has ifiitely may possible types Ad hoc/uiversal? Summary Overloadig Parametric coercio Parametric polymorphism Ad hoc Ad hoc Uiversal Coclusio There are may more pheomea that people call polymorphism. We preseted oly 3 examples ad gave a defiitio that covers them. Laguages with dyamic type checkig do ot eed polymorphism. Polymorphism is a way to gai some freedom ad flexibility ad still beefit from the static type checkig Polymorphism is powerful ad flexible feature but it presets opportuities for abuse. Exercises 1.(Weber Ch.8 ex.1) try yourself at home 2.(Weber Ch.8 ex.3) (i class) Cosider a ukow laguage with iteger ad real types i which 1+2, 1.0+2, 1+2.0 ad 1.0 + 2.0 are all legal expressios. a. Explai how this could be the result of coercio, usig o overloadig b. Explai how this could be the result of overloadig usig o coercio c. Explai how this could result from a combiatio of overloadig ad coercio 7

Outlie Outlie Part II. Fuctioal programmig. A third look at ML More patter matchig Fuctio values ad aoymous fuctios Higher-order fuctios ad curryig Some ML predefied higher-order fuctios More Patter-Matchig Match Sytax Last time we saw patter-matchig i fuctio defiitios: A rule: <rule> ::= <patter> => <expressio> fu f 0 = "zero" f _ = "o-zero"; A match cosists of oe or more rules separated by a vertical bar, like this: <match> ::= <rule> <rule> ' ' <match> Case Expressios Example - case 1+1 of = 3 => "three" = 2 => "two" = _ => "hmm"; val it = "two" : strig case x of _::_::c::_ => c _::b::_ => b a::_ => a il => 0 8

Geeralizes if if exp 1 the exp 2 else exp 3 case exp 1 of true => exp 2 false => exp 3 Behid the Scees Expressios usig if are actually treated as abbreviatios for case expressios This explais some odd SML/NJ error messages: The two expressios above are equivalet So if-the-else is really just a special case of case - if 1=1 the 1 else 1.0; Error: types of rules do't agree [literal] earlier rule(s): bool -> it this rule: bool -> real i rule: false => 1.0 Outlie Predefied Fuctios More patter matchig Fuctio values ad aoymous fuctios Higher-order fuctios ad curryig Predefied higher-order fuctios Whe a ML laguage system starts, there are may predefied variables Some are boud to fuctios: - ord; val it = f : char -> it - ~; val it = f : it -> it Defiig Fuctios Fuctio Values We have see the fu otatio for defiig ew amed fuctios You ca also defie ew ames for old fuctios, usig val just as for other kids of values: - val x = ~; val x = f : it -> it - x 3; val it = ~3 : it Fuctios i ML do ot have ames Just like other kids of values, fuctio values may be give oe or more ames by bidig them to variables The fu sytax does two separate thigs: Creates a ew fuctio value Bids that fuctio value to a ame 9

Aoymous Fuctios Named fuctio: fu - fu f x = x + 2; val f = f : it -> it - f 1; val it = 3 : it Aoymous fuctio: f - f x => x + 2; val it = f : it -> it - (f x => x + 2) 1; val it = 3 : it Usig Aoymous Fuctios whe you eed a small fuctio i just oe place ad you wat to avoid clutterig With amed fuctio - fu itbefore (a,b) = a < b; val itbefore = f : it * it -> bool - quicksort ([1,4,3,2,5], itbefore); val it = [1,2,3,4,5] : it list With aoymous fuctio: - quicksort ([1,4,3,2,5], f (a,b) => a<b); val it = [1,2,3,4,5] : it list - quicksort ([1,4,3,2,5], f (a,b) => a>b); val it = [5,4,3,2,1] : it list The op keyword - op *; val it = f : it * it -> it - quicksort ([1,4,3,2,5], op <); val it = [1,2,3,4,5] : it list Biary operators are special fuctios The keyword op before a operator extracts the fuctio used by the operator Outlie Higher-order Fuctios More patter matchig Fuctio values ad aoymous fuctios Higher-order fuctios ad curryig Predefied higher-order fuctios Every fuctio has a order: A fuctio that does ot take ay fuctios as parameters, ad does ot retur a fuctio value, has order 1 A fuctio that takes a fuctio as a parameter or returs a fuctio value has order +1, where is the order of its highest-order parameter or retured value The quicksort we just saw is a secodorder fuctio 10

Practice What is the order of fuctios with each of the followig ML types? it * it -> bool it list * (it * it -> bool) -> it list it -> it -> it (it -> it) * (it -> it) -> (it -> it) it -> bool -> real -> strig Curryig I ML fuctios have oly oe parameter. Q: How to pass 2 parameters to a fuctio? A1: By passig a 2-tuple: fu f (a,b) = a + b; A2: By curryig = write a fuctio that takes the first argumet, ad returs aother fuctio that takes the secod argumet ad returs the fial result: fu g a = f b => a+b; Haskell B. Curry, (1900-1982) FP mathematicia Example - fu f (a,b) = a+b; val f = f : it * it -> it - fu g a = f b => a+b; val g = f : it -> it -> it - f(2,3); val it = 5 : it - g 2 3; val it = 5 : it Remember that fuctio applicatio is leftassociative So g 2 3 meas ((g 2) 3) 11

Advatages No tuples: we write g 2 3 istead of f(2,3) But the real advatage: we get to specialize fuctios for particular iitial parameters - val add2 = g 2; val add2 = f : it -> it - add2 3; val it = 5 : it - add2 10; val it = 12 : it Advatages - quicksort (op <) [1,4,3,2,5]; val it = [1,2,3,4,5] : it list - val sortbackward = quicksort (op >); val sortbackward = f : it list -> it list - sortbackward [1,4,3,2,5]; val it = [5,4,3,2,1] : it list Multiple Curried Parameters Curryig geeralizes to ay umber of parameters - fu f (a,b,c) = a+b+c; val f = f : it * it * it -> it - fu g a = f b => f c => a+b+c; val g = f : it -> it -> it -> it - f (1,2,3); val it = 6 : it - g 1 2 3; val it = 6 : it Easier Notatio for Curryig Istead of writig: fu f a = f b => a+b; We ca just write: fu f a b = a+b; This geeralizes for ay umber of curried argumets - fu f a b c d = a+b+c+d; val f = f : it -> it -> it -> it -> it Outlie ML Predefied Higher-Order Fuctios More patter matchig map Fuctio values ad aoymous fuctios Higher-order fuctios ad curryig foldr foldl Predefied higher-order fuctios 12

The map Fuctio Used to apply a fuctio to every elemet of a list, ad collect a list of results - map ~ [1,2,3,4]; val it = [~1,~2,~3,~4] : it list - map (f x => x+1) [1,2,3,4]; val it = [2,3,4,5] : it list - map (f x => x mod 2 = 0) [1,2,3,4]; val it = [false,true,false,true] : bool list - map (op +) [(1,2),(3,4),(5,6)]; val it = [3,7,11] : it list What is the type of map? The map Fuctio Is Curried The foldr Fuctio - map; val it = f : ('a -> 'b) -> 'a list -> 'b list - val f = map (op +); val f = f : (it * it) list -> it list - f [(1,2),(3,4)]; val it = [3,7] : it list Use map fuctio whe the result is a list of the same legth with the parameter Used to combie all the elemets of a list (starts from right to left) For example, to add up all the elemets of a list x, we could write foldr (op +) 0 x It takes a fuctio f, a startig value c, ad a list x = [x 1,, x ] ad computes: f x f x, f x, f x c ( ( ( ( )) )) 1, 2 1, So foldr (op +) 0 [1,2,3,4] evaluates as 1+(2+(3+(4+0)))=10 Foldr: examples Fuctio start_value list - foldr (op +) 0 [1,2,3,4]; val it = 10 : it - foldr (op * ) 1 [1,2,3,4]; val it = 24 : it - foldr (op ^) "" ["abc","def","ghi"]; val it = "abcdefghi" : strig - foldr (op ::) [5] [1,2,3,4]; val it = [1,2,3,4,5] : it list The foldr Fuctio Is Curried - foldr; val it = f : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (op +); val it = f : it -> it list -> it - foldr (op +) 0; val it = f : it list -> it - val addup = foldr (op +) 0; val addup = f : it list -> it - addup [1,2,3,4,5]; val it = 15 : it 13

The foldl Fuctio The foldl Fuctio Used to combie all the elemets of a list Same results as foldr i some cases - foldl (op +) 0 [1,2,3,4]; val it = 10 : it - foldl (op * ) 1 [1,2,3,4]; val it = 24 : it To add up all the elemets of a list x, we could write foldl (op +) 0 x It takes a fuctio f, a startig value c, ad a list x = [x 1,, x ] ad computes: f x f x, f x, f x c ( ( ( ( )) )), 1 2 1, So foldl (op +) 0 [1,2,3,4] evaluates as 4+(3+(2+(1+0)))=10 Remember, foldr did 1+(2+(3+(4+0)))=10 The foldl Fuctio foldl starts at the left, foldr starts at the right Differece does ot matter whe the fuctio is associative ad commutative, like + ad * For other operatios, it does matter - foldr (op ^) "" ["abc","def","ghi"]; val it = "abcdefghi" : strig - foldl (op ^) "" ["abc","def","ghi"]; val it = "ghidefabc" : strig - foldr (op -) 0 [1,2,3,4]; val it = ~2 : it - foldl (op -) 0 [1,2,3,4]; val it = 2 : it Exercises (Weber, Ch. 9) Exercise 3. Write a fuctio squarelist of type it list -> it list that takes a list of itegers ad returs the list of squares of those itegers. Exercise 17. Write a fuctio max of type it list ->it that returs the largest elemet of a list. Your fuctio does ot eed to behave well if the list is empty. 14