H ASKELL Session 2 Ronald L. Ramos Proglan DLS-CSB 2 nd Term SY2012-13.

Slides:



Advertisements
Similar presentations
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
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.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Programming for Linguists An Introduction to Python 24/11/2011.
JavaScript Lecture 6 Rachel A Ober
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Language Find the latest version of this document at
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Recursion.
Polymorphic Functions
EECS 110: Lec 10: Definite Loops and User Input
Laziness and Infinite Datastructures
Recursion.
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Chapter 1, Part I.
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Arrays, For loop While loop Do while loop
EECS 110: Lec 10: Definite Loops and User Input
Plus a few random things
Fundamentals of Functional Programming
RECURSION Haskell.
Functions and patterns
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Plus a few random things
“Everything Else”.
1D Arrays and Lots of Brackets
Plus a few random things
PROGRAMMING IN HASKELL
Plus a few random things
Plus a few random things
Introduction to Computer Science
Presentation transcript:

H ASKELL Session 2 Ronald L. Ramos Proglan DLS-CSB 2 nd Term SY

F UNCTIONS

T HE MAIN FUNCTION Try this in codepad: main = putStrLn "Hello, World!"

M AKING YOU OWN F UNCTIONS In codepad.org try this: myFunction x = x + x main = print (myFunction 6) Python: def myFunction(x): return x+x Javascript: function myFunction(x) { return x+x; } In HaskellIn other Languages

M ORE ON F UNCTIONS Call functions from other functions: * myFunction x = x + x anotherFunction x = myFunction x main = print (anotherFunction 6) More complex functions: ** myComplexFunction x = if x > 100 then x else x*2 main = print(myComplexFunction 6)

L OOPS IN H ASKELL Haskell has no loops because it doesn’t need them. There is no “for” or “while” in Haskel Haskell uses recursion

L OOPS SAMPLE fac n = foldl (*) 1 [1..n] main = print (fac 3) In JavaScript var f = new Array(); function factorial (n){ if (n==0 || n==1){ return 1; if(f[n]>0) return f[n]; else return f[n]=factorial(n-1)*n; } In Python def fact(x): if x == 0: return 1 else: return x * fact(x-1) if (len(sys.argv)>1) : print fact(int(sys.argv[1])) else: print Usage In HaskellOther Languages

A N I NTRO TO L ISTS

L ISTS In Haskell, lists are a homogenous data structure. It stores several elements of the same type. That means that we can have a list of integers or a list of characters but we can't have a list that has a few integers and then a few characters.

C REATING A LIST Try this: let lostNumbers = [4,8,15,16,23,42]

P UTTING TWO L ISTS T OGETHER A common task is putting two lists together. This is done by using the ++ operator. [1,2,3,4] ++ [9,10,11,12] "hello" ++ " " ++ "world" ['w','o'] ++ ['o','t'] Putting something at the beginning of the list: 'A':" SMALL CAT" 5:[1,2,3,4,5]

N OTES ON L ISTS [], [[]] and [[],[],[]] are all different things. The first one is an empty list, the second one is a list that contains one empty list, the third one is a list that contains three empty lists.

G ETTING ELEMENTS OF A LIST If you want to get an element out of a list by index, use !! The indices start at 0. “Anne Curtis" !! 6 [9.4,33.2,96.2,11.2,23.25] !! 1

L ISTS WITHIN L ISTS let b = [[1,2,3,4],[5,3,3,3],[1,2,2,3,4],[1,2,3]] b ++ [[1,1,1,1]] [6,6,6]:b b !! 2

C OMPARING L ISTS [3,2,1] > [2,1,0] [3,2,1] > [2,10,100] [3,2,1] > [4,10,100] [3,4,2] > [3,4] [3,4,2] > [2,4] [3,4,2] == [3,4,2]

C OMMON L IST F UNCTIONS head takes a list and returns its head. The head of a list is basically its first element. head [5,4,3,2,1] tail takes a list and returns its tail. In other words, it chops off a list's head. tail [5,4,3,2,1] last takes a list and returns its last element. last [5,4,3,2,1] init takes a list and returns everything except its last element. init [5,4,3,2,1]

C OMMON L IST F UNCTIONS length takes a list and returns its length. length [5,4,3,2,1] null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs ) null [1,2,3] reverse reverses a list reverse [5,4,3,2,1] take takes number and a list. It extracts that many elements from the beginning of the list. take 3 [5,4,3,2,1] take 0 [6,6,6]

C OMMON L IST F UNCTIONS drop drops the number of elements from the beginning of a list drop 3 [8,4,2,1,5,6] maximum takes a list of stuff that can be put in some kind of order and returns the biggest element. maximum [1,9,2,3,4] minimum returns the smallest. minimum [8,4,2,1,5,6]

C OMMON L IST F UNCTIONS sum takes a list of numbers and returns their sum. sum [5,2,1,6,3,2,5,7] product takes a list of numbers and returns their product. product [6,2,1,2] elem takes a thing and a list of things and tells us if that thing is an element of the list. It's usually called as an infix function because it's easier to read that way. 10 `elem` [3,4,5,6]

R ANGES

W HAT ARE R ANGES ? Ranges are a way of making lists that are arithmetic sequences of elements that can be enumerated. Numbers can be enumerated. One, two, three, four, etc. Characters can also be enumerated. The alphabet is an enumeration of characters from A to Z. Names can't be enumerated. What comes after "John"? I don't know.

C REATING A R ANGE Try the following: [1..20] ['a'..'z'] ['K'..'Z'] Specifying a step: [2,4..20] [3,6..20] You cannot do: [1,2,4,8, ] and expect all the powers of 2 [20..1] you have to do [20,19..1]. Be careful of: [0.1, ]

I NFINITE L ISTS [13,26..24*13] is better written as: take 24 [13,26..] Because Haskell is lazy, it won't try to evaluate the infinite list immediately because it would never finish. It'll wait to see what you want to get out of that infinite lists. And here it sees you just want the first 24 elements and it gladly obliges.

I NFINITE L IST F UNCTIONS cycle takes a list and cycles it into an infinite list. If you just try to display the result, it will go on forever so you have to slice it off somewhere. take 10 (cycle [1,2,3]) take 12 (cycle "LOL ") repeat takes an element and produces an infinite list of just that element. It's like cycling a list with only one element. take 10 (repeat 5)

H ASKELL End of Session 2