CS 341 : Programming Languages

Slides:



Advertisements
Similar presentations
C++ Introduction.
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Joe Hummel, PhD Visiting Researcher: U. of California, Irvine Adjunct Professor: U. of Illinois, Chicago & Loyola U., Chicago Materials:
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Joe Hummel, the compiler is at your service Chicago Code Camp 2014.
Pointers OVERVIEW.
1 CS161 Introduction to Computer Science Topic #13.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014.
11 Introduction to Object Oriented Programming (Continued) Cats.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Joe Hummel, the compiler is at your service Chicago Coder Conference, June 2016.
Software Engineering Algorithms, Compilers, & Lifecycle.
Foundations of Programming: Java
Repetitive Structures
EGR 2261 Unit 11 Pointers and Dynamic Variables
Topic: Programming Languages and their Evolution + Intro to Scratch
CS1022 Computer Programming & Principles
Representation, Syntax, Paradigms, Types
A Lecture for the c++ Course
Chapter 7 Part 1 Edited by JJ Shepherd
Functional Programming in a Nutshell with Haskell and F#
The C++ Algorithm Libraries
Computer science By/ Midhat Mohiey. Introduction to Programming using C ++ 2.
MIS Professor Sandvig MIS 324 Professor Sandvig
Announcements Final Exam on August 17th Wednesday at 16:00.
One-Dimensional Array Introduction Lesson xx
CS1100 Computational Engineering
Programming Funamental slides
CS212: Object Oriented Analysis and Design
Representation, Syntax, Paradigms, Types
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Wednesday 09/23/13.
Representation, Syntax, Paradigms, Types
CS150 Introduction to Computer Science 1
Announcements Final Exam on December 25th Monday at 16:00.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Representation, Syntax, Paradigms, Types
Reading from and Writing to Files
Subject:Object oriented programming
Reading from and Writing to Files Part 2
Reading from and Writing to Files
IS 135 Business Programming
Presentation transcript:

CS 341 : Programming Languages Functional Programming in a Nutshell Joe Hummel, PhD joe@joehummel.net http://www.joehummel.net/downloads.html Chicago Coder Conference 2017

CS 341 : Programming Languages Example: Standard Deviation Chicago Coder Conference 2017 Joe Hummel, PhD

Can you find the error(s)? int main() { ifstream file; vector<double> values; double value; // input data: file.open("data.txt"); while (!file.eof()) file >> value; values.push_back(value); } // compute mean: double sum = 0.0; int N = values.size(); for (int i = 0; i <= N; i++) sum = sum + values[i]; double mean = sum / N; Std Dev in C/C++ Can you find the error(s)? // diff^2: forall x do (x - mean)^2 for (int i = 0; i <= N; i++) values[i] = pow(values[i]-mean,2); // std dev = SQRT(mean of diffs) for (int i = 0; i <= N; ++i) sum = sum + values[i]; double result = sqrt(sum / N); // output result: cout << "std dev: " << result; cout << endl; return 0; }

Std Dev in F# 2 data.txt [“2”,“4”,“4”,“4”,“5”,“5”,“7”,“9”] file: #light [<EntryPoint>] let main argv = let file = seq { for line in System.IO.File.ReadLines("data.txt") -> line } let values = Seq.map (fun s -> System.Double.Parse(s)) file // let mean = Seq.average values let diffs = Seq.map (fun x -> System.Math.Pow(x-mean, 2.0)) values let result = System.Math.Sqrt( Seq.average diffs ) printfn "std dev: %A" result [“2”,“4”,“4”,“4”,“5”,“5”,“7”,“9”] file: [2.0,4.0,4.0,4.0,5.0,5.0,7.0,9.0] values: [9.0,1.0,1.0,1.0,0.0,0.0,4.0,16.0] diffs: 2

Observation… No variables!!! Work with “values” --- once assigned, they never change Transform values from what you have to what you want Chicago Coder Conference 2017 Joe Hummel, PhD

CS 341 : Functional Programming From Luca Bolognese’s PDC 2008 talk “An Introduction to F#” X i = i + 1 A slide from Luca Bolognese’s talk at PDC 2008. Opening slide, great talk (still available on the web). In essence, while this statement makes sense to most developers, it makes no sense to mathematicians. Why do we continue to work in this imperative style, when it goes against the grain of so much of what we do? Or try to do (e.g. parallel programming)? ==> enter functional / declarative programming as a better way… p = &z *p = … Joe Hummel, PhD

CS 341 : Programming Languages Why functional programming? Many reasons, but the main ones are… More declarative More time declaring what you want, and less time specifying how to do it Lack of side-effects Easier to reason about program correctness Easier to run in parallel on modern multicore hardware Chicago Coder Conference 2017 Joe Hummel, PhD

Functional programming is a state of mind You don’t need a functional language (though it helps) Here’s a functional version of std dev in C++: int main() { . // Seq.average: const auto mean = std::accumulate(V.begin(), V.end(), 0.0) / V.size(); vector<double> V2; V2.resize( V.size() ); // Seq.map: const auto diffs = std::transform(V.begin(), V.end(), V2.begin(), [=](double x){ return pow(x-mean,2.0);}); const auto result = sqrt( std::accumulate(V2.begin(),V2.end(),0.0) / V2.size() ); Chicago Coder Conference 2017 Joe Hummel, PhD

What algorithm does this code perform What algorithm does this code perform? The @ operator concatenates two lists, yielding a new list. [ 30; 18; 78; 42; 12 ] let rec algorithm L = match L with | [] -> [] | e::[] -> [e] | e::L2 -> let (LTE, GT) = List.partition (fun x -> x <= e) L2 (algorithm LTE) @ [e] @ (algorithm GT) Some kind of search Some kind of sort Computes the median (middle value) Reverses the elements of the list

Program-wide type inference with static type checking… One of the benefits… Program-wide type inference with static type checking… let rec quicksort L = match L with | [] -> [] | e::[] -> [e] | e::L2 -> let (LTE, GT) = List.partition (fun x -> x <= e) L2 (quicksort LTE) @ [e] @ (quicksort GT) Chicago Coder Conference 2017 Joe Hummel, PhD

Functional Programming Styles… let rec quicksort L = match L with | [] -> [] | e::[] -> [e] | e::L2 -> let (LTE, GT) = List.partition (fun x -> x <= e) L2 (quicksort LTE) @ [e] @ (quicksort GT) #1 Higher-order… #2 Recursion Chicago Coder Conference 2017 Joe Hummel, PhD

Why F#? Modern syntax as compared to other FP languages Fully-interoperable with .NET library and languages Tool support ― supported by Visual Studio Practical ― offers variables and side-effects when needed 2002: research on F# starts 2005: v1.0 released 2010: v2.0 released, integrated into Visual Studio 2013: v3.0 released

Interop with C# F# is a .NET language Seamlessly interops with other .NET languages Example: Have F# code return a sequence, which appears as IEnumerable to C# Foreach through results var results = FSLibrary.SomeFunction(...); foreach (var result in results) { this.listBox1.Items.Add(result.ToString()); } Chicago Coder Conference 2017 Joe Hummel, PhD

Demo Chicago crime analysis

Demo Asian options pricing simulation

Summary Functional programming requires a new way of thinking Functional programming is: Easier to reason about correctness Easier to parallelize Gaining traction: financials, scientific computing, parallel community Functional programming is not: As efficient as imperative programming (but getting very close) Taught as widely as imperative programming (but getting better) Chicago Coder Conference 2017 Joe Hummel, PhD

Thank you for attending I live here in Chicago, professor & avid sailor Feel free to reach out: Email: joe@joehummel.net Materials: http://www.joehummel.net/downloads.html Microsoft’s main site for F#: http://msdn.microsoft.com/en-us/vstudio/hh388569 Learning F#: MSR (Microsoft Research): http://www.tryfsharp.org/ MSDN mag: http://msdn.microsoft.com/en- us/magazine/ee336127.aspx Chicago Coder Conference 2017 Joe Hummel, PhD

Deterministic resource management Functional languages Object-oriented Static Dynamic Garbage-collected Deterministic resource management Functional