Topic 7: Algebraic Data Types

Similar documents
COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSC324 Principles of Programming Languages

Tokens, Expressions and Control Structures

Advanced features of Functional Programming (Haskell)

Topic 8: Lazy Evaluation

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

CMSC 330: Organization of Programming Languages

CS 320: Concepts of Programming Languages

Functional Programming and Haskell

Topic 9: Type Checking

Topic 9: Type Checking

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

CS 360: Programming Languages Lecture 12: More Haskell

Inductive Data Types

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

INTRODUCTION TO HASKELL

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

An introduction introduction to functional functional programming programming using usin Haskell

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

CSC324 Principles of Programming Languages

Inheritance and Interfaces

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Topic 2: Making Decisions

Topic 2: Making Decisions

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

C# Adds Useful Features

Topic 4: Tuples and Lists

Use the scantron sheet to enter the answer to questions (pages 1-6)

Algebraic Types. Chapter 14 of Thompson

Inheritance, and Polymorphism.

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Java Object Oriented Design. CSC207 Fall 2014

CMSC 330: Organization of Programming Languages. OCaml Data Types

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

22c:111 Programming Language Concepts. Fall Types I

CSE 143 Lecture 20. Circle

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

+ Abstract Data Types

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Chapter 5. Binary Trees

CS 457/557: Functional Languages

9/19/2018 Programming Data Structures. Polymorphism And Abstract

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

COMP 202 Java in one week

Programming Paradigms

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

CA341 - Comparative Programming Languages

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

A general introduction to Functional Programming using Haskell

Standard ML. Data types. ML Datatypes.1

Chapter 13. Object Oriented Programming

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

F I N A L E X A M I N A T I O N

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

BASIC ELEMENTS OF A COMPUTER PROGRAM

ITI Introduction to Computing II

Programming in Haskell Aug-Nov 2015

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

Framework Fundamentals

Example: Haskell algebraic data types (1)

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS/ENGRD 2110 SPRING 2018

Lexical and Syntax Analysis. Abstract Syntax

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

ITI Introduction to Computing II

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Haskell An Introduction

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSC/MAT-220: Lab 6. Due: 11/26/2018

Structural polymorphism in Generic Haskell

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

APCS Semester #1 Final Exam Practice Problems

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 10 EECE.3220 Data Structures Fall 2017.

Lecture 19: Recursion

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Chapter 14 Abstract Classes and Interfaces

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

COMPUTER SCIENCE PAPER 1

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

ITI Introduction to Computing II

void insert( Type const & ) void push_front( Type const & )

Inheritance, Polymorphism, and Interfaces

Transcription:

Topic 7: Algebraic Data Types 1

Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11, 14.12, 14.14, 14.21, 14.22 Readings: Chapter 5.3 14.1, 14.2, and 14.3 2

Types What types have we already seen? Base Types: Composite Types: Algebraic types provide a powerful mechanism for defining our own types 3

Enumerated Types Algebraic type that has a finite list of possible values Example: Primary Color Possible values: Red, Green and Blue Haskell Declaration: 4

Enumerated Types The values of an enumerated type are referred to as constructors Type names must begin with an uppercase letter Constructors must begin with an uppercase letter Another Example: Construct an enumerated type for the days of the week 5

Enumerated Types New types can be part of an existing type class Can derive the default implementation Example: Extend our Day type so that it can be tested for equality 6

Enumerated Types New types can be part of an existing type class Can provide its own implementation Example: Further extend our Day type so that when it is converted to a String, the full name of the day is used 7

Enumerated Types Enumerated types can be passed as parameters to functions And returned as results Example: Write a function that returns True for days on the weekend, False otherwise 8

Enumerated Types in Other Languages C Enumeration Example: enum PrimaryColor { RED, GREEN, BLUE }; Integers and enumeration constants can be freely mixed Programmer can select the integer assigned to each enumeration value Java Enumeration Example: enum PrimaryColor { RED, GREEN, BLUE }; Defines a new type Cannot be treated as an integer 9

Enumerated Types in Other Languages Python Must import the Enum class New enumerated type is a subclass of Enum Enumerations are not type safe Comparing two enumerated values from different collections returns False rather than raising a type error 10

Product Types Algebraic type that is similar to a tuple One or more values is provided to the constructor when an instance of the type is created Example: Construct a type that stores an arbitrary color as integer amounts of red, green and blue 11

Product Types Can a color be represented as a tuple? Yes! (Int, Int, Int) Can a triangle be represented as a tuple? Yes! (Int, Int, Int) What s the problem with this arrangement? 12

Product Types What benefit do product types provide? Better type checking Better error messages Better code readability What benefits do tuples provide? Functions like fst, snd, zip, unzip, etc. that can t be applied directly to product types Less typing 13

Product Types Product types can have multiple constructors Each constructor can take a different number of parameters Define a type that represents a shape Circles have a radius Rectangles have a length and width Triangles have lengths for all three of their sides 14

Product Types Write a function that computes the area of a shape 15

Recursive Algebraic Types Recall: A recursive definition defines something in terms of itself We previously applied this to functions It can also be applied to types Recursive algebraic type: A type defined in terms of itself 16

Recursive Algebraic Types Example: How do we represent a node in a binary tree in Java? This is a recursive type 17

Recursive Algebraic Types Example: How do we represent a node in a binary tree in Haskell? Base case: An empty tree Recursive case: A value, a left tree and a right tree 18

Creating a Binary Tree What expression represents this tree? 42 13 19 12 19

Creating a Binary Tree 25 What expression represents this tree? 50 75 60 20

Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that determines the height of a binary tree 21

Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that determines whether or not a value is present in the tree How is the function for a general binary tree different from the function for a binary search tree? 22

Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that inserts a new value into a binary search tree 23

Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that constructs a binary search tree from a list of values A non recursive alternative? 24

Functions on Binary Trees Tree traversals: Pre order In order Post order Level order 25

Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that maps a function over every node in a tree 26

Polymorphic Algebraic Types What s wrong with the binary tree type we just created? How do we overcome this limitation? 27

Polymorphic Algebraic Types Create a polymorphic type for a binary tree How does this type need to be modified for a binary search tree? 28

Polymorphic Algebraic Types Example: Write a polymorphic function that adds an element to a polymorphic binary search tree 29

Polymorphic Algebraic Types Example: Write a function that performs an in order traversal of a polymorphic binary search tree 30

Polymorphic Types in Other Languages A polymorphic binary search tree in C++ template <class T> class Node { private: T data; Node* left; Node* right; }; public: Node(const T &val); ~Node(); void insert(const T &v); string tostring() const; template <class T> void Node<T>::insert(const T &v) { if (v < this >data) { if (this >left == NULL) { this >left = new Node<T>(v); } else { this >left >insert(v); } } else { if (this >right == NULL) { this >right = new Node<T>(v); } else { this >right >insert(v); } } } 31

Polymorphic Types in Other Languages A polymorphic binary search tree in C++ template <class T> string Node<T>::toString() const { stringstream ss; ss << (this >left == NULL? "" : this >left >tostring() + ",") << this >data << (this >right == NULL? "" : "," + this >right >tostring()); return ss.str(); } 32

Summary Algebraic types provide several ways to introduce new user defined types Enumerate types Product types Recursive types Polymorphic recursive types 33