Use an adjacency-list representation: type Graph a = ) ] May we represent a graph in Haskell? It is perhaps most natural to Improve its efficiency further, but we will not discuss those here. Levels of recursion with infinite lists, which comes at a significant Implementation of the Sieve of Eratostenes would be in a language That may not seem so bad, but actually this is far slower than an P, we may in fact begin with the value p 2. Tests every integer N for divisibility by every prime Itself recursively, passing the resulting list.ĭefinition of primes is not terribly efficient, beause it ultimately Removes all multiples of p from the (infinite) list xs. Study the helper function to understand how it works. May now easily solve the Project Euler problem mentioned above: euler2 = sum [n | n not (divides p n)) xs) To Haskell's lazy evaluation this works fine. Sequence is the same as before: > take 10 fibsĪgain, it may seem surprising that this is possible at all. For example: > zipWith (+) Īnd so we can define fibs = 1 : 1 : zipWith (+) fibs (tail fibs) So we can use it to add lists selement by element. So we can generate each element as the sum of theĮlements that are one and two positions earlier.įunction combines elements of two lists using an arbitrary function, That makes sense, because for n >= 3 we have To use a recursive function: fibs = f 1 1 where f a b = a : (f b (a + b))Īlternately, as before we can directly define the sequenceįind a recursive pattern. How can we do that? As before, one way is Nicer approach in Haskell is to first define an infinite list ofĪll Fibonacci numbers. Recurses, simultaneously generating the desired sum and stopping when The function could generate Fibonacci numbers as it One approach, we could write a recursive function using anĪccumulator. Million, find the sum of the even-valued terms. Terms in the Fibonacci sequence whose values do not exceed four Suppose that we'd like to solve Project Euler's To generate the third element, Haskell will add 1 to Second element, Haskell takes the first element of ints, i.e. How can this possibly work? Well, from our definition above HaskellĬertainly knows the first element of ints: it is 1. Have observed that ints = 1 : [i + 1 | i take 10 ints Notice that if we add 1 to every element in ints, we get [2, 3, Languages functions may be recursive, but values may not be. It may at first seem surprising that this is possible, since in most In other words, we may define the list in Haskell program runs the function will actually recurse only as many The function theoretically recurses infinitely, but as a Haskell's lazy evaluation, such a function causes no problem in Languages, a recursive function with no base case willĬause a program to hang, since it can never terminate. Notice that our recursive function f has no base case. It to compute the sum of all integers i with i 2 sum (take While (\i -> i * i take 10 ints For example, [1Īn infinite list of all positive integers. Integers (or another enumerable type) can be unbounded at one end, Our earliest weeks of studying Haskell we saw that a range of Generate only as much of the list as is needed, when it is needed. Search, combinatorial search, and searching in stateĪs you all know, lists in Haskell can be infinite.Ĭapability is a natural consequence of Haskell's lazyĪn infinite list in Haskell is no problem since Haskell will lazily Graph representations, depth-first and breadth-first Our topics this week include infinite lists, There was no lecture or tutorial this week.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |