Presentation is loading. Please wait.

Presentation is loading. Please wait.

Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.

Similar presentations


Presentation on theme: "Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are."— Presentation transcript:

1 Patterns in OCaml functions

2 Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the formal parameters –Formal parameters must be variables (in C) Here's a function call: –total = add (total, 5); –total and 5 are the actual parameters –Actual parameters typically can be expressions

3 Parameters are patterns In most conventional languages, formal parameters must be variables –Example: x and y in f(x, y) are both variables In OCaml, all functions take a single parameter For example, in let add (x, y) = x + y;; we say that (x, y) is one parameter, a tuple But (x, y) is not a variable......it is a pattern

4 The unit as parameter Consider: let five ( ) = 5;; The single parameter is the unit, ( ) But the unit is a value, that is, a constant In OCaml, a formal parameter (not just an actual parameter) may be a constant Parameter transmission uses pattern matching

5 Patterns separate cases in Prolog Prolog passes parameters by unification Unification is a very general and powerful kind of pattern matching Parameters are used to separate a task into cases, for example, –member(X, [X | _]). member(X, [_ | Y]) :- member(X, Y).

6 Patterns separate cases in Java Java doesn't do pattern matching on parameters, but... Java does allow methods to be overloaded Overloaded methods have different signatures –The signature describes the number and type of parameters This is a primitive kind of pattern matching In a sense, it separates method calls into cases

7 OCaml allows multiple patterns Pattern matching is not guaranteed to succeed It's OK if a Prolog predicate fails, but......an OCaml function must return a value Therefore, OCaml must support multiple patterns in a function definition Also, OCaml must ensure that the patterns are exhaustive, i.e. some pattern must match

8 Matchs A match consists of rules separated by | A rule has the form pattern -> result –pattern1 -> result1 | pattern2 -> result2 |... patternN -> resultN The results must all be of the same type The patterns should be exhaustive A "match" is not a complete expression by itself

9 match expressions A match all by itself isn't a complete expression A match expression has the form: match expression with match Patterns are tried in the order they are given When a match is found, the corresponding result is evaluated and returned as the result If nothing matches, a Match_failure exception is raised We haven't covered exceptions yet

10 The match expression # match 'b' with 'a' -> 1 | 'b' -> 2 | 'c' -> 3;; – Characters 0-47: Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: 'd' - : int = 2

11 Using match in a function # let rec fact n = match n with 0 -> 1 | n -> fact (n - 1) + fact (n - 2);; – val fact : int -> int = Note: rec is required for recursive functions If you try this, use Enter on the numeric pad!

12 Functions with multiple patterns # let rec factorial = function 0 -> 1 | n -> n * factorial (n - 1);; Notice: –The word function replaces the use of match –There is no explicit formal parameter –Names in patterns are used instead of parameters

13 Lists as patterns Use the cons operator :: to form patterns The pattern (x :: xs) matches a nonempty list # let rec listSize = function [] -> 0 | (x::xs) -> 1 + listSize xs;; val listSize : 'a list -> int = # listSize ['a'; 'b'; 'c'];; - : int = 3

14 Exhaustive patterns # let car = function (x::xs) -> x;; Characters 10-31: Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: [ ] val car : 'a list -> 'a = # car ['a'; 'b'; 'c'];; - : char = 'a' OCaml warns you but lets you continue

15 Last element of a list # let rec last = function [x] -> x | (x::xs) -> last xs;; –Characters 15-53: Warning: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: [ ] val last : 'a list -> 'a = # last ["one"; "two"; "three"];; - : string = "three" Notice the use of [x] and (x::xs) as patterns

16 Doubling each element of a list # let rec doubleAll = function [ ] -> [ ] | (x::xs) -> (2 * x) :: doubleAll xs;; val doubleAll : int list -> int list = # doubleAll [1; 2; 3; 4; 5];; - : int list = [2; 4; 6; 8; 10]

17 if...then...else... expressions if expr1 then expr2 else expr3 is actually syntactic sugar for match expr1 with true => expr2 | false => expr3 If you make an error in an if expression, some compilers may report it as an error in a match expression

18 OCaml is purely functional The if and case expressions are special cases of a match; so is fun Arithmetic operators such as + are syntactic sugar for functions –Operators can be used as functions: op + (a, b) In OCaml, "expressions" are really functions, and basically everything is a function

19 The End


Download ppt "Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are."

Similar presentations


Ads by Google