Haskell Examples 10-May-19.

Slides:



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

Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Miranda – A Functional Language By Dan Vasicek 2009/11/15.
12 Haskell.  ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip  Haskell is  Lazy evaluated  Case-sensitive.
C1: Parallel and Perpendicular Lines
Multiplication Rule. A tree structure is a useful tool for keeping systematic track of all possibilities in situations in which events happen in order.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Bell Ringers Week 3-4. Given that the area of a circle is square inches, what is the radius of the circle? a. 5 inches b. 10 inches c. 25 inches d. 625.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
10.3 – Using Permutations and Combinations Permutation: The number of ways in which a subset of objects can be selected from a given set of objects, where.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
4-Jun-16 Haskell Examples. Factorial fact n = if n == 0 then 1 else n * fact (n - 1) fac 0 = 1 fac (n+1) = (n+1)*fac n.
Examples of comparing strings. “ABC” = “ABC”? yes “ABC” = “ ABC”? No! note the space up front “ABC” = “abc” ? No! Totally different letters “ABC” = “ABCD”?
2.6 Solving Quadratic Equations with Complex Roots 11/9/2012.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Permutations and Combinations
Wed 11/4 Lesson 4 – 7 Learning Objective: To solve using quadratic equations Hw: Lesson 4 – 7 WS.
Functional Programming Lecture 5 - Tuples. Packaging several values together Sometimes you need to package up several values into a single object –A function.
Solve by factoring. x² = - 4 – 5x 2,. Solve by factoring. n² = -30 – 11n -4 and -1.
(2 + 1) + 4 = 2 + (1 + 4) Associative Property of Addition.
Word parts that have meaning Word parts that center on a vowel sound and have no meaning.
Page | 1 Practice Test on Topic 18 Complex Numbers Test on Topic 18 Complex Numbers 1.Express the following as complex numbers a + bi (a) (b) 2 
Aim: How do we solve quadratic equation with complex roots? Do Now: 1. Solve for x: 2. Solve for x: 3. Solve for x: HW: p.219 # 6,8,10,12,14 p.241 # 6,14,25.
March 11, 2005 Recursion (Implementation) Satish Dethe
by D. Fisher (2 + 1) + 4 = 2 + (1 + 4) Associative Property of Addition 1.
(2 + 1) + 4 = 2 + (1 + 4) Associative Property of Addition.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
Miranda Programming Language By Bindu H. Vinay. History of Miranda  Miranda was developed in by David Turner  It is currently being marketed.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
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.
Higher-Order Programming: Iterative computation (CTM Section 3
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Haskell Examples 14-Mar-18.
Recursion.
PROGRAMMING IN HASKELL
Do Now: Factor the polynomial. (5.4 worksheet B)
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
Haskell Chapter 1, Part II.
The Quadratic Formula..
Nature of Roots of a Quadratic Equation
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Find the x coordinate using -b/2a, then find the y coordinate.
What are the equations of the following lines?
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
Using Permutations and Combinations
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
Haskell Examples 20-May-19.
Using the Quadratic Formula to Solve Quadratic Equations
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
10.3 – Using Permutations and Combinations
Presentation transcript:

Haskell Examples 10-May-19

Factorial fact n = if n == 0 then 1 else n * fact (n - 1) fac 0 = 1 fac (n+1) = (n+1)*fac n

Quadratic Formula quadsolve a b c | delta < 0 = error "complex roots" | delta == 0 = [-b/(2*a)] | delta > 0 = [-b/(2*a) + radix/(2*a), -b/(2*a) - radix/(2*a)] where delta = b*b - 4*a*c radix = sqrt delta Main> quadsolve 1 4 (-12) [2.0,-6.0]

Permutations perms :: [a] -> [[a]] perms [] = [[]] perms (h:t) = [take n x ++ [h] ++ drop n x | n <- [0..length t], x <- perms t] Main> perms "abc" ["abc","acb","bac","cab","bca","cba"]

a p p l e -> a p p l e h a y s t r i p e -> i p e s t r a y Pig Latin Pig Latin is a child’s “secret language” Rearranges sounds in words depending on whether word begins with a vowel sound a p p l e -> a p p l e h a y s t r i p e -> i p e s t r a y

Pig Latin for single words latinizeWord w = back w ++ front w ++ suffix w where front w = fst (breakWord w) back w = snd (breakWord w) suffix w = if front w == "" then "hay" else "ay" breakWord w = span (`notElem` "aeiou") w Main> latinizeWord "stripe" "ipestray"

Pig Latin for sentences I Main> words "Haskell is fun" ["Haskell","is","fun"] Main> unwords (words "Haskell is fun") "Haskell is fun" Main> unwords (words "Haskell is fun") "Haskell is fun"

Pig Latin for sentences II latinize s = unwords (map latinizeWord (words s)) Main> latinize "Haskell is fun" "askellHay ishay unfay"

unwords unwords :: [String] -> String unwords [] = "" words and unwords are already defined in the Standard Prelude unwords :: [String] -> String unwords [] = "" unwords ws = foldr1 (\w s -> w ++ ' ':s) ws

unwords (again) unwds :: [[Char]] -> [Char] unwds [] = [] unwds (h:t) = h ++ " " ++ unwds t

Fibonacci Numbers Main> zip ['a'..'z'] [1..5] [('a',1),('b',2),('c',3),('d',4),('e',5)] fib = 1:1:[a+b | (a, b) <- zip fib (tail fib)] Main> take 10 fib [1,1,2,3,5,8,13,21,34,55]

The End