Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.

Slides:



Advertisements
Similar presentations
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Advertisements

Racket Introduction CSC270 Pepper major portions credited to
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Programming in Visual Basic
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Program Design and Development
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
CS 201 Functions Debzani Deb.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Gary MarsdenSlide 1University of Cape Town Functional programming in Clean Gary Marsden Semester 2 – 2000.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Introduction to Python
ISBN Chapter 15 Functional Programming Languages.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
ISBN Chapter 15 Functional Programming Languages.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Algorithm Design.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
H ASKELL Session 2 Ronald L. Ramos Proglan DLS-CSB 2 nd Term SY
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© Kenneth C. Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming
Polymorphic Functions
Functional Programming
Functional Programming Languages
dr Robert Kowalczyk WMiI UŁ
Types CSCE 314 Spring 2016.
Haskell Chapter 2.
Haskell Chapter 1, Part I.
Introduction to Computer Science / Procedural – 67130
Primitive Data, Variables, Loops (Maybe)
Haskell.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Arithmetic operations, decisions and looping
Functional Programming
Control Flow Chapter 6.
T. Jumana Abu Shmais – AOU - Riyadh
Building Java Programs
Low Level Programming Languages
Fundamentals of Functional Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
COMPUTING.
Presentation transcript:

Haskell Chapter 1, Part I

Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca

Why are we doing this?  i-want-to-learn-haskell i-want-to-learn-haskell  s-haskell-worth-learning s-haskell-worth-learning

Why study functional languages? Without understanding functional programming, you can’t invent MapReduce, the algorithm that makes Google so massively scalable. —Joel Spolsky, The Perils of Java Schools

Imperative Programming Language  The design of the imperative languages is based directly on the von Neumann architecture  Computer follows a sequence of task specified by the programmer  Focus is on state – variables that control the problem solution  Use flow-control to work toward solution  sequence  decision (if/else)  looping (for/while/etc) 0x0000 0x0010 0x0100 0x0110 …. instructions data

Functional Programming Language  Emphasizes functions that provide results  Avoids state and mutable data  Relatively unconcerned with the architecture of the machines on which programs will run  Has its roots in lambda calculus (covered later) info from Wikipedia FUNCTION INPUTS OUTPUTS

Referential Transparency  In a purely functional language, functions have no side effects  As a result, if you call the same function twice with the same parameters, it is guaranteed to produce the same results  This is called referential transparency  Enables you to prove that a function is correct someFunction 545 someFunction 5still (and always) 45… and no other effects Allows Haskell to do memoization

Lazy evaluation  Haskell doesn’t evaluate functions until it needs to show a result  OK with referential transparency… no one is relying on any side effects  Enables infinite data structures…. only parts you want to display are actually computed

Statically typed with type inference  Knows the type of a piece of code at compile time (static)  Won’t allow you to add a string and number, for example  Type system uses type inference. Haskell figures out the type (e.g., a = 5 + 4, a must be a number)  Allows programmers to leave out type declarations but still do type checking  Is not the same as dynamic typing – why not?  Some languages with type inference:  ML, OCaml, F#, Haskell, Scala, D, Clean, Opa and Go.

Haskell is elegant and concise  Can do a lot with a few lines of code… but it might take some getting used to!

Quick Haskell Demo  Simple arithmetic (+, *, -, /, `div`, precedence)  Booleans (True, False, &&, ||, not)  Comparisons (==, /=)  succ/pred

Quick Haskell Demo - continued  definition (let)  Lists [1,2,3]  homogeneous  common to concatenate (++)  cons/add to front ( : )  access element by index ( !! ) [use sparingly, to get more FP feel]  lists of lists  comparing lists  list parts: head, tail, init, last, length  take, drop, maximum, minimum, sum, product, elem  Ranges

Quick Haskell Demo Put function definitions in a file Function names must begin with lowercase!! Load  Function calls  Function definitions  every function must return a value  function names can’t begin with capital letters  use apostrophe at end to denote strict (not lazy) or slightly modified version of a file  if/else  else is required (statement must do something) WinGHCi fine for small examples, mostly you will edit/load files

Play and Share 1. doubleHead takes two lists and returns the product of the heads. doubleHead [2,3] [4,6] => 8 2. longerList takes two lists and returns the longer one. longerList [1,2][4,5,6] => [4,5,6] (your choice which to return if same length) 3. isItThere takes an element and a list and returns “yes” if element is in list, “no” otherwise. isItThere 1 [2,3] => “no” Can you make this work with a string? 4. evenOdd num takes a number n and returns a list containing the first n even numbers followed by the first n odd numbers. evenOdd 4 => [2,4,6,8,1,3,5,7]. You may assume n <= addToTail takes two lists and returns a list with the head of the first list at the end of the second. addToTail [1,2,3] [4,5,6] => [4,5,6,1] 6. replaceLast takes two lists and returns a list with the last element of the second list replaced by the last element of the first list. replaceLast [1,2,3] [4,5,6] => [4,5,3] 7. removeHeads takes two lists and concatenates them, minus the first elements. removeHeads [1,2,3] [4,5,6] => [2,3,5,6] 8. replaceList takes two lists and replaces the first n elements (where n = length of list 1) with the elements of list 1. replaceList [1,2] [4,5,6,7] => [1,2,6,7] replaceList[1,2,3,4] [7,8] => [1,2,3,4] Advanced  grabIt. Takes a string of words, no spaces, e.g., “theblackcat”. Takes a list of word lengths, e.g., [3,5,3]. Takes a word number, e.g., 1, 2 or 3. returns the word. grabIt wordLengths word 2 => “cat”  HINT: You’ll need to use (fromIntegral …) to convert from Integer to Int. More in chapter 2.