ml a strongly typed functional language n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
ML: A Strongly Typed Functional Language PowerPoint Presentation
Download Presentation
ML: A Strongly Typed Functional Language

Loading in 2 Seconds...

play fullscreen
1 / 10

ML: A Strongly Typed Functional Language - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

ML: A Strongly Typed Functional Language. Interpreted (compilers available): -bash-3.00$ poly Poly/ML 5.4 Release > 1+3; val it = 4 : int > o ^D -bash-3.00. ML: A Strongly Typed Functional Language. Strongly typed: > 1.5 + 3; Error-Type error in function application.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'ML: A Strongly Typed Functional Language' - lecea


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
ml a strongly typed functional language
ML: A Strongly Typed Functional Language

Interpreted (compilers available):

-bash-3.00$ poly

Poly/ML 5.4 Release

> 1+3;

val it = 4 : int

>o^D

-bash-3.00

ml a strongly typed functional language1
ML: A Strongly Typed Functional Language

Strongly typed:

> 1.5 + 3;

Error-Type error in function application.

Function: + : real * real -> real

Argument: (1.5, 3) : real * int

Reason:

Can't unify int (*In Basis*) with real (*In Basis*)

(Different type constructors)

Found near 1.5 + 3

Static Errors

ml a strongly typed functional language2
ML: A Strongly Typed Functional Language

Functional:

> fun square(x:int) = x*x;

val square = fn : int -> int

> map square [1,2,3];

val it = [1,4,9] : int list

ml a strongly typed functional language3
ML: A Strongly Typed Functional Language

Type-inferencing:

> fun add(x:int, y) = x+y;

val add = fn : int * int -> int

> fun foo(a, b, c) = if a then b else 1+c;

val foo = fn : bool * int * int -> int

ml a strongly typed functional language4
ML: A Strongly Typed Functional Language

Pattern-matching:

> fun fact(n) = if n = 0 then 1 else n * fact(n-1);

val fact = fn : int -> int

> fact(5);

val it = 120 : int

> fun fact(0) = 1 | fact(n) = n * fact(n-1);

val fact = fn : int -> int

> fact(5);

val it = 120 : int

ml lists
ML: Lists

> val a =[1,3,5,7,9];

val a = [1,3,5,7,9] : int list

> hd(a);

val it = 1 : int

> tl(a);

val it = [3,5,7,9] : int list

> a @ [11];

val it = [1,3,5,7,9,11] : int list

ml lists1
ML: Lists

> fun rev(ls) = if null(ls) then nil

else rev(tl(ls)) @ [hd(ls)];

val rev = fn : 'a list -> 'a list

> rev([1,3,5,7,9]);

val it = [9,7,5,3,1] : int list

>

> fun rev(nil) = nil | rev(x::t) = rev(t) @ [x];

val rev = fn : 'a list -> 'a list

> rev([1,3,5,7,9]);

val it = [9,7,5,3,1] : int list

ml tuples
ML: Tuples

(single, double, triple, quadruple, quintuple,

sextuple, heptuple, octuple, ..., -tuple):

> [1,2,3];

val it = [1,2,3] : int list

> (1,2,3);

val it = (1,2,3) : int * int * int

> [1,2,3.5];

Error-Elements in a list have different types.

> (1,2,3.5);

val it = (1,2,3.5) : int * int * real

ml tuples1

So f(3,4) is a fun of one arg: tuple of type

  • int*int
  • Recall imaginary numbers / complex arithmetic:
  • C = {ai + b}, i = √-1
  • So can represent C as real*real:
  • - fun cmag (a, b) = sqrt(a*a + b*b);
  • val cmag = fn : real * real -> real % how inferred?
  • - cmag (3.0, 4.0);
  • val it = 5.0 : real
ML: Tuples
ml tuples2
ML: Tuples

- fun cadd(a1:real, b1:real) (a2, b2) =

(a1+a2, b1+b2);

val cadd = fn : real * real -> real * real ->

real * real

- cadd (3.3, 4.4) (5.5, 6.6);

val it = (8.8,11.0) : real * real

- fun cadd (a1,b1) (a2,b2) = (a1+a2,b1+b2):real*real;

val cadd = fn : real * real -> real * real ->

real * real