Programming Languages and Compilers (CS 421) - PowerPoint PPT Presentation

About This Presentation
Title:

Programming Languages and Compilers (CS 421)

Description:

Title: Programming Languages and Compilers (CS 421) Author: me Last modified by: Munawar Hafiz Created Date: 8/31/2000 6:37:53 PM Document presentation format – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 8
Provided by: me7746
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages and Compilers (CS 421)


1
Programming Languages and Compilers (CS 421)
  • Munawar Hafiz
  • 2219 SC, UIUC
  • http//www.cs.uiuc.edu/class/cs421/

Based in part on slides by Mattox Beckman, as
updated by Vikram Adve, Gul Agha and Elsa Gunter
2
Exceptions - Example
  • exception Zero
  • exception Zero
  • let rec list_mult_aux list
  • match list with -gt 1
  • x xs -gt
  • if x 0 then raise Zero
  • else x list_mult_aux xs
  • val list_mult_aux int list -gt int ltfungt

3
Exceptions - Example
  • let list_mult list
  • try list_mult_aux list with Zero -gt 0
  • val list_mult int list -gt int ltfungt
  • list_mult 342
  • - int 24
  • list_mult 740
  • - int 0
  • list_mult_aux 740
  • Exception Zero.

4
Exceptions
  • When an exception is raised
  • The current computation is aborted
  • Control is thrown back up the call stack until
    a matching handler is found
  • All the intermediate calls waiting for a return
    value are thrown away

5
Implementing Exceptions
  • let multkp m n k
  • let r m n in
  • (print_string "product result "
  • print_int r print_string "\n"
  • k r)
  • val multkp int -gt int -gt (int -gt 'a) -gt 'a
    ltfungt

6
Implementing Exceptions
  • let rec list_multk_aux list k kexcp
  • match list with -gt k 1
  • x xs -gt if x 0 then kexcp 0
  • else list_multk_aux xs
  • (fun r -gt multkp x r k) kexcp
  • val list_multk_aux int list -gt (int -gt 'a) -gt
    (int -gt 'a) -gt 'a ltfungt
  • let rec list_multk list k list_multk_aux list
    k k
  • val list_multk int list -gt (int -gt 'a) -gt 'a
    ltfungt

7
Implementing Exceptions
  • list_multk 342 report
  • product result 2
  • product result 8
  • product result 24
  • 24
  • - unit ()
  • list_multk 740 report
  • 0
  • - unit ()
Write a Comment
User Comments (0)
About PowerShow.com