Embed Size (px)

### Text of Simple Functions in Haskell

True -> 1 False -> n * fact (n - 1) Another way of writing the same thing is to use Haskell's list type. Lists are a very fundamental type in Haskell. They're similar to lists in Lisp, except that all of the elements of a list must have the same type. A list is written between square brackets, with the values in the list written inside, separated by commas, like: [1, 2, 3, 4, 5] As in lisp, the list is actually formed from pairs, where the first element of the pair is a value in the list, and the second value is the rest of the list. Pairs are constructed in Haskell using ":", so the list could also be written in the following ways: 1 : [2, 3, 4, 5] 1 : 2 : [3, 4, 5] 1 : 2 : 3 : 4 : 5 : [] If you want a list of integers in a specific range, there's a shorthand for it using "..". To generate the list of values from x to y, you can write: [ x .. y ] Getting back to our factorial function, the factorial of a number "n" is the product of all of the integers from 1 to n. So another way of saying that is that the factorial is the result of taking the list of all integers from 1 to n, and multiplying them together: listfact n = listProduct [1 .. n] But that doesn't work, because we haven't defined listProduct yet. Fortunately, Haskell provides a ton of useful list functions. One of them, "foldl", takes a function f, an initial value i, and a list [l1, l2,...,ln], and basically does f(ln(...f(l2, f(i,l1)))). So we can use foldl with the multiply function to multiply the elements of the list together. The only problem is that multiplication is written as an infix operator, not a function. In Haskell, the solution to that is simple: an infix operator is just fancy syntax for a function call; to get the function, you just put the operator into parens. So the multiplication function is written "(*)". So let's add a definition of listProduct using that; we'll do it using a where clause, which allows us to define variables or functions that are local to the scope of the enclosing function definition: listfact n = listProduct [ 1 .. n ] where listProduct lst = foldl (*) 1 lst I need to explain one more list function before moving on. There's a function called "zipWith" for performing an operation pairwise on two lists. For example, given the lists "[1,2,3,4,5]" and "[2,4,6,8,10]", "zipWith (+) [1,2,3,4,5] [2,4,6,8,10]" would result in "[3,6,9,12,15]". Now we're going to jump into something that's going to seem really strange. One of the fundamental properties of how Haskell runs a program is that Haskell is a lazy language. What that means is that no expression is actually evaluated until its value is needed. So you can do things like create infinite lists - since no part of the list is computed until it's needed. So we can do things like define the fibonacci series - the complete fibonacci series, using:

fiblist = 0 : 1 : (zipWith (+) fiblist (tail fiblist)) This looks incredibly strange. But if we tease it apart a bit, it's really pretty simple: 1. The first element of the fibonacci series is "0" 2. The second element of the fibonacci series is "1" 3. For the rest of the series, take the full fibonacci list, and line up the two copies of it, offset by one (the full list, and the list without the first element), and add the values pairwise. That third step is the tricky one. It relies on the laziness of Haskell: Haskell won't compute the nth element of the list until you explicitly reference it; until then, it just keeps around the unevaluated code for computing the part of the list you haven't looked at. So when you try to look at the nth value, it will compute the list only up to the nth value. So the actual computed part of the list is always finite - but you can act as if it wasn't. You can treat that list as if it really were infinite - and retrieve any value from it that you want. Once it's been referenced, then the list up to where you looked is concrete - the computations won't be repeated. But the last tail of the list will always be an unevaluated expression that generates the next pair of the list - and that pair will always be the next element of the list, and an evaluated expression for the pair after it. Just to make sure that the way that "zipWith" is working in "fiblist" is clear, let's look at a prefix of the parameters to zipWith, and the result. (Remember that those three are all actually the same list! The diagonals from bottom left moving up and right are the same list elements.) fiblist = [0 tail fiblist = [1 zipWith (+) = [1 1 1 2 1 2 3 2 3 5 3 5 8 5 8 ...] 8 ... ] 13 ... ]

Given that list, we can find the nth element of the list very easily; the nth element of a list l can be retrieved with "l !! n", so, the fibonacci function to get the nth fibonacci number would be: fib n = fiblist !! n And using a very similar trick, we can do factorials the same way: factlist = 1 : (zipWith (*) [1..] factlist) factl n = factlist !! n The nice thing about doing factorial this way is that the values of all of the factorials less than n are also computed and remember - so the next time you take a factorial, you don't need to repeat those multiplications. ##### Background – Functions (1C) · 1/31/2018  · Functions 8 Young Won Lim 1/31/18 Functions : First-class Data Types functions are first-class data types Haskell treats functions
Documents Documents Documents ##### CSCI 2041: Curried Functions and Closureskauffman/2041/10... · –Wikipedia "Haskell Curry" Source: Wikipedi "Haskell Curry" 7. Currying Enables Partial Application From partial_apply.ml,
Documents ##### Haskell: Lists · 2/24/2017  · Introduction to Haskell —Characteristics: Type System Haskell is a pure functional PL. It has first-class functions and good support for higher-order
Documents ##### Kai Duhrk op · functional decompose problem into a set of functions functions transform input to output functions have no internal state examples: Haskell, Scheme, Clojure 4/38 Kai
Documents ##### Haskell: Programming with Functions Overview of Day 2goto.ucsd.edu/~nvazou/club_de_science15/day2.pdfHaskell: Programming with Functions Niki Vazou, Pablo Serrano Clubes de Ciencia,
Documents ##### An overview of Haskell - Haifux · Introduction Haskell is a pure functional language. It means that: Variables never change after deﬁnition. Functions don’t have side eﬀects
Documents ##### Haskell Syntax in Functions - IGMigm.univ-mlv.fr/~vialette/teaching/2015-2016/Haskell/Lectures/04 Syntax in Functions...Pattern Matching Pattern matching consists of specifying patterns
Documents Documents ##### Structural polymorphism in Generic Haskell · Haskell I Haskell is a statically typed, pure functional language with lazy evaluation. I Functions are deﬁned by pattern matching
Documents ##### Curry time - Learn you a Haskell · Everything in Haskell is evaluated lazily. FunctionalConcepts Functions WorkingwithTypes Examples Brainfuck Section2 Functions. FunctionalConcepts
Documents ##### A Simple Microcontroller VHDL Tutorial R. E. Haskell and D. M. Hanna T6: VHDL State Machines
Documents ##### PROGRAMMING IN HASKELL - math.rs · Haskell Scripts As well as the functions in the standard prelude, you can also define your own functions; New functions are defined within a script,
Documents ##### Haskell 101 - people.engr.tamu.eduHaskell Scripts A Haskell program consists of one or more scripts A script is a text file comprising a sequence of definitions, where new functions
Documents ##### 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers
Documents ##### Haskell Types, Classes, and Functions, Currying, …courses.cs.tamu.edu › ... › cs314 › slides › lec › Lec04.pdf1 CSCE 314 TAMU –Fall 2017Haskell Types, Classes, and Functions,
Documents   