CS15 2014 TShirts! Congratulations to Emily Kasbohm, whose T-Shirt design won! We will set up a Teespring campaign, and email a link to the class if you.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Recursion CS 367 – Introduction to Data Structures.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
A small part of what you should know about continuations, but were too afraid to ask Nick Benton.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS503: First Lecture, Fall 2008 Michael Barnathan.
Computer Science 1620 Programming & Problem Solving.
CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
SM3121 Software Technology Mark Green School of Creative Media.
CS 211 RECURSION TREES. Trees versus Linked Lists A tree is like a linked list, except instead of a single next node, it can have multiple next nodes.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
A quadratic equation is a second degree polynomial, usually written in general form: The a, b, and c terms are called the coefficients of the equation,
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable, class name, method name, etc. Operand: a named memory.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Nachos Phase 1 Code -Hints and Comments
Lists in Python.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS107 References and Arrays By Chris Pable Spring 2009.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
MA 1128: Lecture 17 – 6/17/15 Adding Radicals Radical Equations.
IB Computer Science Unit 5 – Advanced Topics Recursion.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
EGR 2261 Unit 10 Two-dimensional Arrays
Loops BIS1523 – Lecture 10.
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Cracking the Coding Interview
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
CSE 341: Programming Langs
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
ARRAYS 1 GCSE COMPUTER SCIENCE.
CS 201 Fundamental Structures of Computer Science
Coding Concepts (Basics)
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
CISC124 Labs start this week in JEFF 155. Fall 2018
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Last Class We Covered Recursion Stacks Parts of a recursive function:
Presentation transcript:

CS TShirts! Congratulations to Emily Kasbohm, whose T-Shirt design won! We will set up a Teespring campaign, and a link to the class if you would like to order one

Programming Languages and why you should love them

Problem 2.1 Design a program called rainfall that consumes a list of real numbers representing daily rainfall readings. The list may contain the number -999 indicating the end of the data of interest. Produce the average of the non-negative values in the list up to the first -999 (if it shows up). There may be negative numbers other than -999 in the list (representing faulty readings). Assume that there is at least one non-negative number before Example: rainfall takes in an ArrayList containing(1, -2, 5, -999, 8) and returns 3

Solution A public class Rainfall1 { public double rainfall (ArrayList data) { double total = 0; int days = 0 ; for (double d : data) { if (-999 == d) { return (total / days) ; } else if (d >= 0) { total = total + d ; days = days + 1 ; } return (total / days) ; }

Solution C public class Rainfall3 { public ArrayList cleanData (ArrayList data){ ArrayList cleanData = new ArrayList (); for (double d : data) { if (-999 == d) { return(cleanData); } else if (d >= 0) { cleanData.add(d); } return cleanData; }

public double sum(ArrayList data) { double total = 0; for (double d : data) { total = total + d; } return total; } public double rainfall (ArrayList data) { ArrayList relevant = cleanData(data); return(sum(relevant) / relevant.size()); }

Stats breakdown: Historical: rough summary of data from literature since 1986 (all imperative programming) Functional: 218 students over 5 courses, all had a 17-like curriculum Java: 51 students from 1 course, had covered arrays but not lists percentage of students

Why does this happen, and why do we care? The language you learn to program in influences how you approach solving problems

Different languages make some things easier and more efficient to do Experts deciding the “right” way to program in a language take into account things like compiler optimizations and stack space Also considered when building a language: make efficient features easier for a programmer to use –Built in functions become part of problem solving toolkit Bold claim, Ardra…..

Example: Recursion vs. Iteration Most Java programmers’ instincts are to use iteration, while functional programmers rely very heavily on recursion Recursion is slower in Java than in functional languages (like Racket, Haskell, Scala) which optimize recursion to not use more stack space Structure and syntax of a language makes some of its features more efficient and accessible, which changes how you use it. It might not even have all features!

You probably don’t know all this stuff about stack frames and recursion optimization, and I don’t expect you to Most new programmers don’t! The people deciding what is “good” vs. “bad” design do though, which trickles down into standard coding styles and design patterns People building a language also make computationally cheap things easier to do, and expensive ones harder

Functional Programming Focus on computation, doesn’t allow mutation What does that mean? int a = 5 a = 6 NOT ALLOWED – can’t store or change state The whole idea of a method that doesn’t return something seems crazy. What else can it do??

Because you’re only ever creating new information (not changing it), the language can optimize certain features Allows it to store data in memory more efficiently Creation of data structures becomes very cheap. Conscious choice because that’s something they want to encourage! Extreme case of language design encouraging a certain style because it won’t let you use mutation Can’t even use loop counters in the same way!

How does this affect you? Let’s go back to the problem from the homework

Solution A public class Rainfall1 { public double rainfall (ArrayList data) { double total = 0; int days = 0 ; for (double d : data) { if (-999 == d) { return (total / days) ; } else if (d >= 0) { total = total + d ; days = days + 1 ; } return (total / days) ; }

Solution A Single loop If we’ve reached the end of the list return Otherwise, if it’s good information, add it to the total and increment the counter

Solution C public class Rainfall3 { public ArrayList cleanData (ArrayList data){ ArrayList cleanData = new ArrayList (); for (double d : data) { if (-999 == d) { return(cleanData); } else if (d >= 0) { cleanData.add(d); } return cleanData; }

public double sum(ArrayList data) { double total = 0; for (double d : data) { total = total + d; } return total; } public double rainfall (ArrayList data) { ArrayList relevant = cleanData(data); return(sum(relevant) / relevant.size()); }

Three steps: –Cleanse data –Sum data –Average it This and Solution B take similar approaches Students with functional programming backgrounds tend to produce this kind of solution Why??

Languages have built in functions –filter() which could be used to cleanse –sum() to add up list These built in functions are like toolkits –Once they’re in your mind you’re more likely to use them to solve problems –It’s like the language is suggesting that you use them by making it really easy to do so –Start thinking of kinds of problems in terms of their solutions This is just a cleansing problem!

% of students using built-in/higher-order functions filter fold length filter: takes list and predicate, returns list Solution Composition

First solution has less code, depends on two counters, and does a lot of computation inside one for-loop Third solution has more code, takes up more space by taking up a second data structure, and runs through the data twice So the third one is worse, right? Comparing Solutions

MAYBE NOT If programming functionally, creating new data structures is very cheap Easier to debug: can isolate problems to individual methods and test that they work Cleanly broken into 3 conceptual parts –Good for understanding, good for reuse! –3 small tasks instead of one big one More extensible: if you ever want to use this data again, you would still have to clean it and could reuse your cleanseData() function. But in the first you’d have to write entirely new code!

If we wanted to write it the second way but not use more space, we could use cleanseData to remove bad input instead of creating a new list Return type? Would functional programmers do this? No!!! Relies on mutation, mistakes easier to make and you catch them later –If cleanseData() returns an empty list you know you’ve got a problem, but if it deletes the wrong things the bug is harder to track down

When methods change the state of the program you have no guarantee about the your variables after the method call –It could have deleted everything and you’d have no idea! Without mutation, it doesn’t matter how a method does something as long as it has right return value Separates “what” from “how” – good encapsulation! So, use mutation if you really really need it, but not as a default Makes sense when you’re manipulating huge matrices of data (like for graphics), but often it doesn’t

Takeaway: Another way to approach problems: prioritizing the extensibility and clarity of your design instead of just pure space/number of times you look at the data Keep in mind that the most efficient solution may depend on the language you’re working in – there’s a reason background affects what you think is best! Different approaches can inform your thought process and help you develop better solutions –Some problems are best solved with one kind of solution, and a language can lend itself to multiple paradigms

Mutation vs. none is just one of many choices programming languages make Can also not have types! –May seem like a small thing, but fundamentally changes your language –Inheritance? Polymorphism? –Say goodbye! Every programming language makes hundreds of choices about which features to include and how to implement them. –And then the choices interact with each other –It’s very intricate!!

Which is why programming can be hard Every language is going to do things differently, but you should be able to pick up new ones fairly quickly because you’ve learned how to learn a language Once you understand the high level choices made in a language and the school of thought it’s in, reading writing and understanding it gets way easier –Which is why you should learn more about programming language theory!!

Closing: Learning different languages gives you whole new ways to look at problems, and whole new toolkits for tackling them! You’ve spent this whole semester learning our style of programming, but it’s only one way of looking at problems. Learning more languages and understanding their paradigms and how they’re put together will make you a much better programmer. –And it’s really freakin cool!!