Introduction to Objective Caml

Total Page:16

File Type:pdf, Size:1020Kb

Introduction to Objective Caml Introduction to Objective Caml Jason Hickey DRAFT. DO NOT REDISTRIBUTE. Copyright © Jason Hickey, 2008. This book has been submitted for publication by Cambridge University Press. This draft may be used until the time the book appears in print. January 11, 2008 Copyright © Jason Hickey 2 Draft. Do not redistribute. Contents Preface i 1 Introduction 1 1.1 Functional and imperative languages .................. 3 1.2 Organization ............................... 3 1.3 Additional Sources of Information ................... 3 2 Simple Expressions 5 2.1 Comment convention .......................... 5 2.2 Basic expressions ............................ 5 2.2.1 unit: the singleton type .................... 6 2.2.2 int: the integers ........................ 6 2.2.3 float: the floating-point numbers ............... 7 2.2.4 char: the characters ...................... 8 2.2.5 string: character strings .................... 8 2.2.6 bool: the Boolean values .................... 9 2.3 Operator precedences .......................... 10 2.4 The OCaml type system ......................... 11 2.5 Compiling your code .......................... 12 2.6 Exercises ................................ 14 3 Variables and Functions 15 3.1 Functions ................................ 16 3.1.1 Scoping and nested functions . 18 3.1.2 Recursive functions ....................... 18 3.1.3 Higher order functions ..................... 19 3.2 Variable names ............................. 20 3.3 Labeled parameters and arguments ................... 21 3.3.1 Rules of thumb ......................... 22 3.4 Exercises ................................ 24 4 Basic pattern matching 29 4.1 Functions with matching ........................ 30 4.2 Pattern expressions ........................... 31 3 CONTENTS CONTENTS 4.3 Values of other types .......................... 31 4.4 Incomplete matches ........................... 33 4.5 Patterns are everywhere ......................... 34 4.6 Exercises ................................ 35 5 Tuples, lists, and polymorphism 37 5.1 Polymorphism .............................. 37 5.1.1 Value restriction ........................ 38 5.1.2 Other kinds of polymorphism . 39 5.2 Tuples .................................. 41 5.3 Lists ................................... 42 5.4 Tail recursion .............................. 44 5.4.1 Optimization of tail-recursion . 44 5.4.2 Lists and tail recursion ..................... 45 5.5 Exercises ................................ 47 6 Unions 49 6.1 Binary trees ............................... 51 6.2 Unbalanced binary trees ........................ 51 6.3 Unbalanced, ordered, binary trees ................... 52 6.4 Balanced red-black trees ........................ 53 6.5 Open union types (polymorphic variants) . 55 6.5.1 Type definitions for open types . 56 6.5.2 Closed union types ....................... 57 6.6 Some common built-in unions ..................... 57 6.7 Exercises ................................ 58 7 Reference cells and side-effects 61 7.1 Pure functional programming ...................... 62 7.1.1 Value restriction ........................ 64 7.2 Queues ................................. 64 7.3 Doubly-linked lists ........................... 65 7.4 Memoization .............................. 67 7.5 Graphs .................................. 69 7.6 Exercises ................................ 73 8 Records, Arrays, and String 77 8.1 Records ................................. 77 8.1.1 Functional and imperative record updates . 78 8.1.2 Field label namespace ..................... 79 8.2 Arrays .................................. 80 8.3 Strings .................................. 80 8.4 Hash tables ............................... 81 8.5 Exercises ................................ 84 Copyright © Jason Hickey 4 Draft. Do not redistribute. CONTENTS CONTENTS 9 Exceptions 87 9.1 Nested exception handlers ....................... 89 9.2 Examples of uses of exceptions ..................... 90 9.2.1 The exception Not_found ................... 90 9.2.2 Invalid_argument and Failure . 90 9.2.3 Pattern matching failure .................... 91 9.2.4 Assertions ............................ 92 9.2.5 Memory exhaustion exceptions . 92 9.3 Other uses of exceptions ........................ 93 9.3.1 Decreasing memory usage ................... 93 9.3.2 Break statements ........................ 93 9.3.3 Unwind-protect (finally) .................... 94 9.3.4 The exn type .......................... 95 9.4 Exercises ................................ 96 10 Input and Output 99 10.1 File opening and closing ........................ 99 10.2 Writing and reading values on a channel . 100 10.3 Channel manipulation . 101 10.4 String buffers .............................. 102 10.5 Formatted output with Printf . 103 10.6 Formatted input with Scanf . 105 10.7 Exercises ................................ 106 11 Files, Compilation Units, and Programs 109 11.1 Single-file programs . 109 11.1.1 Where is the main function? . 109 11.1.2 OCaml compilers . 111 11.2 Multiple files and abstraction . 111 11.2.1 Defining an interface . 112 11.2.2 Transparent type definitions . 114 11.3 Some common errors . 115 11.3.1 Interface errors . 115 11.4 Using open to expose a namespace . 117 11.4.1 A note about open . 118 11.5 Debugging a program . 118 11.6 Exercises ................................ 121 12 The OCaml Module System 125 12.1 Structures and signatures . 125 12.2 Module definitions . 127 12.2.1 Modules are not first-class . 128 12.2.2 The let module expression . 128 12.3 Recursive modules . 129 12.4 The include directive . 130 12.4.1 Using include to extend modules . 130 Copyright © Jason Hickey 5 Draft. Do not redistribute. CONTENTS CONTENTS 12.4.2 Using include to extend implementations . 130 12.5 Abstraction, friends, and module hiding . 132 12.5.1 Using include with incompatible signatures . 133 12.6 Sharing constraints . 134 12.7 Exercises ................................ 136 13 Functors 139 13.1 Sharing constraints . 141 13.2 Module sharing constraints . 141 13.3 Module re-use using functors . 143 13.4 Higher-order functors . 145 13.5 Recursive modules and functors . 145 13.6 A complete example . 146 13.7 Exercises ................................ 150 14 Objects 155 14.1 Encapsulation and polymorphism . 156 14.2 Transformations ............................. 158 14.2.1 Basis transformations . 158 14.2.2 Functional update . 159 14.3 Binary methods ............................. 160 14.4 Object factories ............................. 160 14.5 Imperative objects ............................ 161 14.6 self: referring to the current object . 163 14.7 Initializers; private methods . 164 14.8 Object types, coercions, and subtyping . 165 14.8.1 Coercions ............................ 166 14.8.2 Subtyping ............................ 168 14.9 Narrowing ................................ 170 14.9.1 Why narrowing is bad . 171 14.9.2 Implementing narrowing . 172 14.10Alternatives to objects . 172 14.11Exercises ................................ 174 15 Classes and inheritance 179 15.1 Class basics ............................... 179 15.1.1 Class types . 180 15.1.2 Parameterized classes . 181 15.1.3 Classes with let-expressions . 181 15.1.4 Type inference . 182 15.2 Inheritance ............................... 183 15.2.1 Method override . 185 15.2.2 Class types . 186 15.2.3 Class type constraints and hiding . 187 15.2.4 Classes and class types as object types . 189 15.3 Inheritance is not subtyping . 190 Copyright © Jason Hickey 6 Draft. Do not redistribute. CONTENTS CONTENTS 15.4 Modules and classes . 193 15.5 Polymorphic methods and virtual classes . 195 15.5.1 Polymorphic methods . 196 15.5.2 Virtual (abstract) classes and methods . 198 15.5.3 Terminology . 199 15.5.4 Stacks .............................. 199 15.5.5 Lists and stacks . 200 15.6 Exercises ................................ 202 16 Multiple inheritance 209 16.1 Examples of multiple inheritance . 209 16.1.1 Inheriting from multiple independent classes . 210 16.1.2 Inheriting from multiple virtual classes . 210 16.1.3 Mixins ............................. 211 16.2 Overriding and shadowing . 213 16.3 Repeated inheritance . 214 16.4 Avoiding repeated inheritance . 216 16.4.1 Is-a vs. has-a . 216 16.4.2 Mixins revisited . 218 16.5 Exercises ................................ 219 17 Polymorphic Classes 221 17.1 Polymorphic dictionaries . 221 17.1.1 Free type variables in polymorphic classes . 222 17.1.2 Instantiating a polymorphic class . 222 17.1.3 Inheriting from a polymorphic class . 223 17.2 Polymorphic class types . 224 17.2.1 Coercing polymorphic classes . 226 17.2.2 Variance annotations . 228 17.2.3 Positive and negative occurrences . 230 17.2.4 Coercing by hiding . 231 17.3 Type constraints ............................. 232 17.4 Comparing objects and modules . 234 17.4.1 Late binding . 234 17.4.2 Extending the definitions . 236 17.5 Exercises ................................ 242 Syntax 251 .1 Notation ................................. 251 .2 Terminal symbols (lexemes) . 252 .2.1 Whitespace and comments . 253 .2.2 Keywords ............................ 253 .2.3 Prefix and infix symbols . 254 .2.4 Integer literals . 254 .2.5 Floating-point literals . 255 .2.6 Character literals . 255 Copyright © Jason Hickey 7 Draft. Do not redistribute. CONTENTS CONTENTS .2.7 String literals . 255 .2.8 Identifiers ............................ 256 .2.9 Labels .............................. 256 .2.10 Miscellaneous . 256 .3 Names .................................. 256 .3.1 Simple names . 256 .3.2 Path names . 257 .4 Expressions ..............................
Recommended publications
  • Configuring UNIX-Specific Settings: Creating Symbolic Links : Snap
    Configuring UNIX-specific settings: Creating symbolic links Snap Creator Framework NetApp September 23, 2021 This PDF was generated from https://docs.netapp.com/us-en/snap-creator- framework/installation/task_creating_symbolic_links_for_domino_plug_in_on_linux_and_solaris_hosts.ht ml on September 23, 2021. Always check docs.netapp.com for the latest. Table of Contents Configuring UNIX-specific settings: Creating symbolic links . 1 Creating symbolic links for the Domino plug-in on Linux and Solaris hosts. 1 Creating symbolic links for the Domino plug-in on AIX hosts. 2 Configuring UNIX-specific settings: Creating symbolic links If you are going to install the Snap Creator Agent on a UNIX operating system (AIX, Linux, and Solaris), for the IBM Domino plug-in to work properly, three symbolic links (symlinks) must be created to link to Domino’s shared object files. Installation procedures vary slightly depending on the operating system. Refer to the appropriate procedure for your operating system. Domino does not support the HP-UX operating system. Creating symbolic links for the Domino plug-in on Linux and Solaris hosts You need to perform this procedure if you want to create symbolic links for the Domino plug-in on Linux and Solaris hosts. You should not copy and paste commands directly from this document; errors (such as incorrectly transferred characters caused by line breaks and hard returns) might result. Copy and paste the commands into a text editor, verify the commands, and then enter them in the CLI console. The paths provided in the following steps refer to the 32-bit systems; 64-bit systems must create simlinks to /usr/lib64 instead of /usr/lib.
    [Show full text]
  • Mod Perl 2.0 Source Code Explained 1 Mod Perl 2.0 Source Code Explained
    mod_perl 2.0 Source Code Explained 1 mod_perl 2.0 Source Code Explained 1 mod_perl 2.0 Source Code Explained 15 Feb 2014 1 1.1 Description 1.1 Description This document explains how to navigate the mod_perl source code, modify and rebuild the existing code and most important: how to add new functionality. 1.2 Project’s Filesystem Layout In its pristine state the project is comprised of the following directories and files residing at the root direc- tory of the project: Apache-Test/ - test kit for mod_perl and Apache2::* modules ModPerl-Registry/ - ModPerl::Registry sub-project build/ - utilities used during project build docs/ - documentation lib/ - Perl modules src/ - C code that builds libmodperl.so t/ - mod_perl tests todo/ - things to be done util/ - useful utilities for developers xs/ - source xs code and maps Changes - Changes file LICENSE - ASF LICENSE document Makefile.PL - generates all the needed Makefiles After building the project, the following root directories and files get generated: Makefile - Makefile WrapXS/ - autogenerated XS code blib/ - ready to install version of the package 1.3 Directory src 1.3.1 Directory src/modules/perl/ The directory src/modules/perl includes the C source files needed to build the libmodperl library. Notice that several files in this directory are autogenerated during the perl Makefile stage. When adding new source files to this directory you should add their names to the @c_src_names vari- able in lib/ModPerl/Code.pm, so they will be picked up by the autogenerated Makefile. 1.4 Directory xs/ Apache2/ - Apache specific XS code APR/ - APR specific XS code ModPerl/ - ModPerl specific XS code maps/ - tables/ - Makefile.PL - 2 15 Feb 2014 mod_perl 2.0 Source Code Explained 1.4.1 xs/Apache2, xs/APR and xs/ModPerl modperl_xs_sv_convert.h - modperl_xs_typedefs.h - modperl_xs_util.h - typemap - 1.4.1 xs/Apache2, xs/APR and xs/ModPerl The xs/Apache2, xs/APR and xs/ModPerl directories include .h files which have C and XS code in them.
    [Show full text]
  • Types and Programming Languages by Benjamin C
    < Free Open Study > . .Types and Programming Languages by Benjamin C. Pierce ISBN:0262162091 The MIT Press © 2002 (623 pages) This thorough type-systems reference examines theory, pragmatics, implementation, and more Table of Contents Types and Programming Languages Preface Chapter 1 - Introduction Chapter 2 - Mathematical Preliminaries Part I - Untyped Systems Chapter 3 - Untyped Arithmetic Expressions Chapter 4 - An ML Implementation of Arithmetic Expressions Chapter 5 - The Untyped Lambda-Calculus Chapter 6 - Nameless Representation of Terms Chapter 7 - An ML Implementation of the Lambda-Calculus Part II - Simple Types Chapter 8 - Typed Arithmetic Expressions Chapter 9 - Simply Typed Lambda-Calculus Chapter 10 - An ML Implementation of Simple Types Chapter 11 - Simple Extensions Chapter 12 - Normalization Chapter 13 - References Chapter 14 - Exceptions Part III - Subtyping Chapter 15 - Subtyping Chapter 16 - Metatheory of Subtyping Chapter 17 - An ML Implementation of Subtyping Chapter 18 - Case Study: Imperative Objects Chapter 19 - Case Study: Featherweight Java Part IV - Recursive Types Chapter 20 - Recursive Types Chapter 21 - Metatheory of Recursive Types Part V - Polymorphism Chapter 22 - Type Reconstruction Chapter 23 - Universal Types Chapter 24 - Existential Types Chapter 25 - An ML Implementation of System F Chapter 26 - Bounded Quantification Chapter 27 - Case Study: Imperative Objects, Redux Chapter 28 - Metatheory of Bounded Quantification Part VI - Higher-Order Systems Chapter 29 - Type Operators and Kinding Chapter 30 - Higher-Order Polymorphism Chapter 31 - Higher-Order Subtyping Chapter 32 - Case Study: Purely Functional Objects Part VII - Appendices Appendix A - Solutions to Selected Exercises Appendix B - Notational Conventions References Index List of Figures < Free Open Study > < Free Open Study > Back Cover A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute.
    [Show full text]
  • Scala−−, a Type Inferred Language Project Report
    Scala−−, a type inferred language Project Report Da Liu [email protected] Contents 1 Background 2 2 Introduction 2 3 Type Inference 2 4 Language Prototype Features 3 5 Project Design 4 6 Implementation 4 6.1 Benchmarkingresults . 4 7 Discussion 9 7.1 About scalac ....................... 9 8 Lessons learned 10 9 Language Specifications 10 9.1 Introdution ......................... 10 9.2 Lexicalsyntax........................ 10 9.2.1 Identifiers ...................... 10 9.2.2 Keywords ...................... 10 9.2.3 Literals ....................... 11 9.2.4 Punctions ...................... 12 9.2.5 Commentsandwhitespace. 12 9.2.6 Operations ..................... 12 9.3 Syntax............................ 12 9.3.1 Programstructure . 12 9.3.2 Expressions ..................... 14 9.3.3 Statements ..................... 14 9.3.4 Blocksandcontrolflow . 14 9.4 Scopingrules ........................ 16 9.5 Standardlibraryandcollections. 16 9.5.1 println,mapandfilter . 16 9.5.2 Array ........................ 16 9.5.3 List ......................... 17 9.6 Codeexample........................ 17 9.6.1 HelloWorld..................... 17 9.6.2 QuickSort...................... 17 1 of 34 10 Reference 18 10.1Typeinference ....................... 18 10.2 Scalaprogramminglanguage . 18 10.3 Scala programming language development . 18 10.4 CompileScalatoLLVM . 18 10.5Benchmarking. .. .. .. .. .. .. .. .. .. .. 18 11 Source code listing 19 1 Background Scala is becoming drawing attentions in daily production among var- ious industries. Being as a general purpose programming language, it is influenced by many ancestors including, Erlang, Haskell, Java, Lisp, OCaml, Scheme, and Smalltalk. Scala has many attractive features, such as cross-platform taking advantage of JVM; as well as with higher level abstraction agnostic to the developer providing immutability with persistent data structures, pattern matching, type inference, higher or- der functions, lazy evaluation and many other functional programming features.
    [Show full text]
  • Cisco Telepresence ISDN Link API Reference Guide (IL1.1)
    Cisco TelePresence ISDN Link API Reference Guide Software version IL1.1 FEBRUARY 2013 CIS CO TELEPRESENCE ISDN LINK API REFERENCE guide D14953.02 ISDN Link API Referenec Guide IL1.1, February 2013. Copyright © 2013 Cisco Systems, Inc. All rights reserved. 1 Cisco TelePresence ISDN Link API Reference Guide ToC - HiddenWhat’s in this guide? Table of Contents text The top menu bar and the entries in the Table of Introduction ........................................................................... 4 Description of the xConfiguration commands ......................17 Contents are all hyperlinks, just click on them to go to the topic. About this guide ...................................................................... 5 Description of the xConfiguration commands ...................... 18 User documentation overview.............................................. 5 We recommend you visit our web site regularly for Technical specification ......................................................... 5 Description of the xCommand commands .......................... 44 updated versions of the user documentation. Support and software download .......................................... 5 Description of the xCommand commands ........................... 45 What’s new in this version ...................................................... 6 Go to:http://www.cisco.com/go/isdnlink-docs Description of the xStatus commands ................................ 48 Automatic pairing mode ....................................................... 6 Description of the
    [Show full text]
  • Typedef in C
    CC -- TTYYPPEEDDEEFF http://www.tutorialspoint.com/cprogramming/c_typedef.htm Copyright © tutorialspoint.com The C programming language provides a keyword called typedef, which you can use to give a type a new name. Following is an example to define a term BYTE for one-byte numbers: typedef unsigned char BYTE; After this type definitions, the identifier BYTE can be used as an abbreviation for the type unsigned char, for example:. BYTE b1, b2; By convention, uppercase letters are used for these definitions to remind the user that the type name is really a symbolic abbreviation, but you can use lowercase, as follows: typedef unsigned char byte; You can use typedef to give a name to user defined data type as well. For example you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows: #include <stdio.h> #include <string.h> typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } Book; int main( ) { Book book; strcpy( book.title, "C Programming"); strcpy( book.author, "Nuha Ali"); strcpy( book.subject, "C Programming Tutorial"); book.book_id = 6495407; printf( "Book title : %s\n", book.title); printf( "Book author : %s\n", book.author); printf( "Book subject : %s\n", book.subject); printf( "Book book_id : %d\n", book.book_id); return 0; } When the above code is compiled and executed, it produces the following result: Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 typedef vs #define The #define is a C-directive which is also used to define the aliases for various data types similar to typedef but with following differences: The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, like you can define 1 as ONE etc.
    [Show full text]
  • Technical Data Specifications & Capacities
    5669 (supersedes 5581)-0114-L9 1 Technical Data Specifications & Capacities Crawler Crane 300 Ton (272.16 metric ton) CAUTION: This material is supplied for reference use only. Operator must refer to in-cab Crane Rating Manual and Operator's Manual to determine allowable crane lifting capacities and assembly and operating procedures. Link‐Belt Cranes 348 HYLAB 5 5669 (supersedes 5581)-0114-L9 348 HYLAB 5 Link‐Belt Cranes 5669 (supersedes 5581)-0114-L9 Table Of Contents Upper Structure ............................................................................ 1 Frame .................................................................................... 1 Engine ................................................................................... 1 Hydraulic System .......................................................................... 1 Load Hoist Drums ......................................................................... 1 Optional Front-Mounted Third Hoist Drum................................................... 2 Boom Hoist Drum .......................................................................... 2 Boom Hoist System ........................................................................ 2 Swing System ............................................................................. 2 Counterweight ............................................................................ 2 Operator's Cab ............................................................................ 2 Rated Capacity Limiter System .............................................................
    [Show full text]
  • The Scala Experience Safe Programming Can Be Fun!
    The Scala Programming Language Mooly Sagiv Slides taken from Martin Odersky (EPFL) Donna Malayeri (CMU) Hila Peleg (Technion) Modern Functional Programming • Higher order • Modules • Pattern matching • Statically typed with type inference • Two viable alternatives • Haskel • Pure lazy evaluation and higher order programming leads to Concise programming • Support for domain specific languages • I/O Monads • Type classes • ML/Ocaml/F# • Eager call by value evaluation • Encapsulated side-effects via references • [Object orientation] Then Why aren’t FP adapted? • Education • Lack of OO support • Subtyping increases the complexity of type inference • Programmers seeks control on the exact implementation • Imperative programming is natural in certain situations Why Scala? (Coming from OCaml) • Runs on the JVM/.NET • Can use any Java code in Scala • Combines functional and imperative programming in a smooth way • Effective libraries • Inheritance • General modularity mechanisms The Java Programming Language • Designed by Sun 1991-95 • Statically typed and type safe • Clean and Powerful libraries • Clean references and arrays • Object Oriented with single inheritance • Interfaces with multiple inheritance • Portable with JVM • Effective JIT compilers • Support for concurrency • Useful for Internet Java Critique • Downcasting reduces the effectiveness of static type checking • Many of the interesting errors caught at runtime • Still better than C, C++ • Huge code blowouts • Hard to define domain specific knowledge • A lot of boilerplate code •
    [Show full text]
  • Unix/Linux Command Reference
    Unix/Linux Command Reference .com File Commands System Info ls – directory listing date – show the current date and time ls -al – formatted listing with hidden files cal – show this month's calendar cd dir - change directory to dir uptime – show current uptime cd – change to home w – display who is online pwd – show current directory whoami – who you are logged in as mkdir dir – create a directory dir finger user – display information about user rm file – delete file uname -a – show kernel information rm -r dir – delete directory dir cat /proc/cpuinfo – cpu information rm -f file – force remove file cat /proc/meminfo – memory information rm -rf dir – force remove directory dir * man command – show the manual for command cp file1 file2 – copy file1 to file2 df – show disk usage cp -r dir1 dir2 – copy dir1 to dir2; create dir2 if it du – show directory space usage doesn't exist free – show memory and swap usage mv file1 file2 – rename or move file1 to file2 whereis app – show possible locations of app if file2 is an existing directory, moves file1 into which app – show which app will be run by default directory file2 ln -s file link – create symbolic link link to file Compression touch file – create or update file tar cf file.tar files – create a tar named cat > file – places standard input into file file.tar containing files more file – output the contents of file tar xf file.tar – extract the files from file.tar head file – output the first 10 lines of file tar czf file.tar.gz files – create a tar with tail file – output the last 10 lines
    [Show full text]
  • Enum, Typedef, Structures and Unions CS 2022: Introduction to C
    Enum, Typedef, Structures and Unions CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh Cornell University (based on slides by Saikat Guha) Fall 2009, Lecture 6 Enum, Typedef, Structures and Unions CS 2022, Fall 2009, Lecture 6 Numerical Types I int: machine-dependent I Standard integers I defined in stdint.h (#include <stdint.h>) I int8 t: 8-bits signed I int16 t: 16-bits signed I int32 t: 32-bits signed I int64 t: 64-bits signed I uint8 t, uint32 t, ...: unsigned I Floating point numbers I float: 32-bits I double: 64-bits Enum, Typedef, Structures and Unions CS 2022, Fall 2009, Lecture 6 Complex Types I Enumerations (user-defined weekday: sunday, monday, ...) I Structures (user-defined combinations of other types) I Unions (same data, multiple interpretations) I Function pointers I Arrays and Pointers of the above Enum, Typedef, Structures and Unions CS 2022, Fall 2009, Lecture 6 Enumerations enum days {mon, tue, wed, thu, fri, sat, sun}; // Same as: // #define mon 0 // #define tue 1 // ... // #define sun 6 enum days {mon=3, tue=8, wed, thu, fri, sat, sun}; // Same as: // #define mon 3 // #define tue 8 // ... // #define sun 13 Enum, Typedef, Structures and Unions CS 2022, Fall 2009, Lecture 6 Enumerations enum days day; // Same as: int day; for(day = mon; day <= sun; day++) { if (day == sun) { printf("Sun\n"); } else { printf("day = %d\n", day); } } Enum, Typedef, Structures and Unions CS 2022, Fall 2009, Lecture 6 Enumerations I Basically integers I Can use in expressions like ints I Makes code easier to read I Cannot get string equiv.
    [Show full text]
  • 1 Ocaml for the Masses
    PROGRAMMING LANGUAGES OCaml for the Masses Why the next language you learn should be functional Yaron Minsky, Jane Street Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function. - John Carmack Functional programming is an old idea with a distinguished history. Lisp, a functional language inspired by Alonzo Church’s lambda calculus, was one of the first programming languages developed at the dawn of the computing age. Statically typed functional languages such as OCaml and Haskell are newer, but their roots go deep—ML, from which they descend, dates back to work by Robin Milner in the early ’70s relating to the pioneering LCF (Logic for Computable Functions) theorem prover. Functional programming has also been enormously influential. Many fundamental advances in programming language design, from garbage collection to generics to type inference, came out of the functional world and were commonplace there decades before they made it to other languages. Yet functional languages never really made it to the mainstream. They came closest, perhaps, in the days of Symbolics and the Lisp machines, but those days seem quite remote now. Despite a resurgence of functional programming in the past few years, it remains a technology more talked about than used. It is tempting to conclude from this record that functional languages don’t have what it takes. They may make sense for certain limited applications, and contain useful concepts to be imported into other languages; but imperative and object-oriented languages are simply better suited to the vast majority of software engineering tasks.
    [Show full text]
  • Evaluation of On-Ramp Control Algorithms
    CALIFORNIA PATH PROGRAM INSTITUTE OF TRANSPORTATION STUDIES UNIVERSITY OF CALIFORNIA, BERKELEY Evaluation of On-ramp Control Algorithms Michael Zhang, Taewan Kim, Xiaojian Nie, Wenlong Jin University of California, Davis Lianyu Chu, Will Recker University of California, Irvine California PATH Research Report UCB-ITS-PRR-2001-36 This work was performed as part of the California PATH Program of the University of California, in cooperation with the State of California Business, Transportation, and Housing Agency, Department of Transportation; and the United States Department of Transportation, Federal Highway Administration. The contents of this report reflect the views of the authors who are responsible for the facts and the accuracy of the data presented herein. The contents do not necessarily reflect the official views or policies of the State of California. This report does not constitute a standard, specification, or regulation. Final Report for MOU 3013 December 2001 ISSN 1055-1425 CALIFORNIA PARTNERS FOR ADVANCED TRANSIT AND HIGHWAYS Evaluation of On-ramp Control Algorithms September 2001 Michael Zhang, Taewan Kim, Xiaojian Nie, Wenlong Jin University of California at Davis Lianyu Chu, Will Recker PATH Center for ATMS Research University of California, Irvine Institute of Transportation Studies One Shields Avenue University of California, Davis Davis, CA 95616 ACKNOWLEDGEMENTS Technical assistance on Paramics simulation from the Quadstone Technical Support sta is also gratefully acknowledged. ii EXECUTIVE SUMMARY This project has three objectives: 1) review existing ramp metering algorithms and choose a few attractive ones for further evaluation, 2) develop a ramp metering evaluation framework using microscopic simulation, and 3) compare the performances of the selected algorithms and make recommendations about future developments and eld tests of ramp metering systems.
    [Show full text]