I have a code
g :: Int->Int->Int
g x y = x*2 - y
then If i call foldl1 g [4,3,2,1] it returns 15, but i don't get how it returns 15, can anyone explain me why this is the case?
foldl1 first applies the function to the first two elements of the list, then takes the result and applies the function to it and the third element, then takes the result and applies the function to it and the fourth element, then to result and fifth element, then sixth element, and so on until the list ends.
So:
Step 1: g 4 3 = 4*2 - 3 = 5
Step 2: g 5 2 = 5*2 - 2 = 8
Step 3: g 8 1 = 8*2 - 1 = 15
Related
sum :: [Int] -> Int
sum [] = 0
sum (x:xs) = x + sum xs
I do not understand what the purpose of the line sum [] = 0 is. I found this piece of code in a textbook (it did not go into detail as to what this line does.)
sum [] = 0 is the edge condition of the recursion.
Suppose we have sum [1, 4, 6, 8]. The list can be rewritten as 1:4:6:8:[]. The empty list is the last 'element'. The calculation is as follow (1 + ( 4 + (6 + ( 8 + 0)))). When all the elements of the list have been traversed, what remains is the empty list. Adds zero to the calculated result and ends the iteration.
My question is :
I have a list and a number and I want to sum the number to the list so I can do this
Adding ls n = [x+n| x<-ls]
and it works.
My question is I want to add n+1, n+2, n+3 depending of the length of the list.
If I do
let b = 0
Adding´ ls n = [x+adder n b| x<-ls] where adder n b= n+b,b++
it doesn't work because the b doesn't advance, so if I have Adding´ [1,3,4] 3 = [4,7,9] .
You may use Data.List.mapAccumL (mapAccumL :: Traversable t => (a -> b -> (a, c)) -> a -> t b -> (a, t c)) to achieve this task.
The first parameter is a function which takes two parameters a (accumulator) and n (the list item) and returns a tuple. In our case we increment a (the accumulator) by 1 and map the current element n by adding the accumulator. The result is a tuple in which the first item is the final state of the accumulator and the second is the final state of the list. We extract the second item by snd.
Prelude> snd $ Data.List.mapAccumL (\a n -> (a+1,n+a)) 3 [1,3,4]
[4,7,9]
Approach the problem recursively. Add a number to the first element of the list, then recurse on the tail of the list with the next larger number. Repeat until you are out of numbers. Instead of incrementing b, you start a new function call in which b has a larger value.
adding [] _ = []
adding (x:xs) b = x + b : adding xs (b+1)
As an example, consider
adding [7, 10, 7, 5] 0 == 7 + 0 : adding [10, 7, 5] 1
== 7 : (10 + 1 : adding [7, 5] 2)
== 7 : 11 : (7 + 2 : adding [5] 3)
== 7 : 11 : 9 : (5 + 3 : adding [] 4)
== 7 : 11 : 9 : 8 : []
== [7, 11, 9, 8]
You can replace 0 with any starting value n in the initial call; the recursive call always increments it.
Haskell allows to represent recurrent functions in a very concise way. For example, infinite list, that contains Fibonacci numbers can be defined as follows:
fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
I am dealing with 'probabilists' Hermite polynomials, which have the following recursion relation:
What would be the optimal way to construct the infinite list of n-th Hermite polynomials for given x?
We can write it as:
hermite :: (Enum n, Num n) => n -> [n]
hermite x = s
where s#(_:ts) = 1 : x : zipWith3 (\hn2 hn1 n1 -> x*hn1 - n1*hn2) s ts [1..]
where the first items 1 : x : ... are the first elements of the hermite (you can fill in other values).
For the next one, we zip the original values s (so that starts with H0), the tail ts of s (that starts with H1) and the index (that starts with 2, 3, ...) and perform an operation x*hn1 - x*hn2 (nh1 stands for Hn-1, and nh2 stands for Hn-2), and so we calculate the next element each time.
The first 11 values for x = 0.75 are:
Prelude> take 11 (hermite 0.75)
[1.0,0.75,-0.4375,-1.828125,-5.859375e-2,7.2685546875,5.744384765625,-39.30303955078125,-69.68797302246094,262.1583366394043,823.8105096817017]
So the first value is 1, the second x, the third one x*x-2, the fourth one x*x*x-2*x-3*x, and so on.
That being said, if I recall correctly, the recursion formula of the Hermite polynomials is:
Hn(x) = 2×x×Hn-1(x)-2×(n-1)Hn-2(x)
instead of the one quoted in the question.
In that case the formula is thus:
hermite :: (Enum n, Num n) => n -> [n]
hermite x = s
where s#(_:ts) = 1 : 2 * x : zipWith3 helper s ts [1..]
helper hn2 hn1 n1 = 2 * (x * hn1 - n1 * hn2)
Then the first 11 values are:
Prelude> take 11 (hermite 0.75)
[1.0,1.5,0.25,-5.625,-9.9375,30.09375,144.515625,-144.3515625,-2239.74609375,-1049.994140625,38740.4384765625]
Which is correct acording to this Wolfram article:
H0 = 1
H1 = 2*x
H2 = 4˙x2 - 2
H3 = 8˙x3 - 4˙x
H4 = 16˙x4 - 48˙x2 + 12
Which maps exactly on the values we obtained:
Prelude> let x = 0.75 in [1,2*x,4*x*x-2,8*x*x*x-4*x,16*x*x*x*x-48*x*x+12]
[1.0,1.5,0.25,0.375,-9.9375]
I have some Haskell code and need to write down Leftmost-innermost (call by value) and outermost (call-by-name):
second :: [Int] -> Int
second [] = 0
second (_:[]) = 0
second (_:x:xs) = x
doubleEach :: [Int] -> [Int]
doubleEach [] = []
doubleEach (x:xs) = x * 2 : (doubleEach xs)
repeat :: Int -> Int -> [Int]
repeat x n = if n > 0 then x : (repeat x (n-1)) else []
repeat ( second (doubleEach [2,3,5] )) ( second [3, 1, 4] )
innermost (call-by-value):
1. repeat (second (doubleEach [2,3,5] )) (second [3,1,4])
2. repeat (second 4 : doubleEach [3,5])) (1)
3. repeat (second (4 : 6 : d [5])) (1)
4. repeat (second ( 4 : 6 : 10 )) (1)
5. repeat (6) (1)
6. [6]
Question: How can I get outermost (call-by-name) stept by step evaluation? I do not understand how I can do that if repeat needs specified values to work and they are not given until the inner part is not evaluated.
How can I get outermost (call-by-name) stept by step evaluation? I do not understand how I can do that if repeat needs specified values to work and they are not given until the inner part is not evaluated.
You don't need the values to expand the function call - you can just pass in the unevaluated expressions. And then you only evaluate those expressions when it's necessary for ifs, pattern matches or primitive functions. That's how call-by-name evaluation works.
So your first step would be to take the body of repeat, replace each occurrence of x with second (doubleEach [2,3,5]) and each occurrence of n with second [3, 1, 4]. Then you'll need to evaluate the if condition and then you proceed with the body of the if.
I'm new to Haskell and I really need some help!
I have to write a program that includes a recursive function to produce a list of binomial coefficients for the power n=12 using the Pascal's triangle technique.
I have some ideas in my head but because I'm just getting started I have no idea how to implement this to haskell?!
Could someone please help me out??
first row: (a+b)^0 = 1
second row: (a+b)^1 = 1a+1b
third row: (a+b)^2 = 1a^2+2ab+1b^2
and so on...this is my main idea. But I cant even try this out because I have no idea how I put this in Haskell..getting errors all the time
Start by assigning an index to each element in the triangle:
| 0 1 2 3 4 5 6
--+--------------------------
0 | 1
1 | 1 1
2 | 1 2 1
3 | 1 3 3 1
4 | 1 4 6 4 1
5 | 1 5 10 10 5 1
6 | 1 6 15 20 15 6 1
Here I've simply put the triangle on its side so that we can number them. So here I'd say that the element at (6, 4) is 15, whereas (4, 6) doesn't exist. Now focus on writing a function
pascal :: Integer -> Integer -> Integer
pascal x y = ???
Such that you can generate this version of the triangle. You can start by writing
pascal x y
| x == 0 = 1
| x == y = 1
| x < y = error "Not a valid coordinate for Pascal's triangle."
| otherwise = pascal ? ? + pascal ? ?
Note that here, instead of figuring out which elements should be added together by diagonals, you can do it via rectangular coordinates. Here, you'll note that y is which row in the triangle you're on and x is the position of the element in that row. All you need to do is figure out what goes in place of the ?s.
Once you get that working, I've got a one-liner for this triangle that is more efficient and can generate the entire triangle all at once while still using recursion:
import Data.List (scanl1)
pascals :: [[Integer]]
pascals = repeat 1 : map (scanl1 (+)) pascals
Don't try turning this solution in to your professor, it's not what they're looking for and it would make it pretty obvious someone gave you this solution if you've only been doing Haskell for a week. However, it really shows how powerful Haskell can be for this sort of problem. I would show how to index pascals to get a given (n, k) value, but doing so would also give you too many hints for solving the naive recursion.
Since there's been some confusion, the reason why I gave this solution is to draw a parallel between it and the often shown lazy implementation for the Fibonacci sequence:
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
Compared to
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
This definition generates an infinite list of all the Fibonacci numbers, and does so quite efficiently (from the point of view of the CPU, RAM is a different story). It encodes in its first 2 elements the base case, then a recursive expression that can calculate the rest. For the Fibonaccis, you need 2 values to start you off, but for Pascal's triangle, you only need one value, that value just happens to be an infinite list. There is an easy to see pattern going across the columns in the grid I posted above, the scanl1 (+) function just takes advantage of this pattern and allows us to generate it very easily, but this is generating the diagonals of the triangle rather than the rows. To get the rows, you can index this list, or you can do some fancy tricks with take, drop, and other such functions, but that's an exercise for another day.
Start out with the triangle itself:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
You should notice that to write down the next row, you must apply this rule: sum the previous rows' adjacent elements, using a 0 for the lonely edge elements. Visually:
0 1 0
\+/ \+/
0 1 1 0
\+/ \+/ \+/
0 1 2 1 0
\+/ \+/ \+/ \+/
1 3 3 1
...
Operationally, that looks like this:
For row 0:
[1] (it's a given; i.e. base case)
For row 1:
[0, 1] <- row 0 with a zero prepended ([0] ++ row 0)
+ +
[1, 0] <- row 0 with a zero appended (row 0 ++ [0])
= =
[1, 1] <- element-wise addition
For row 2:
[0, 1, 1]
+ + +
[1, 1, 0]
= = =
[1, 2, 1]
Generally, for row N:
element-wise addition of:
[0] ++ row(N-1)
row(N-1) ++ [0]
Remember that element-wise addition of lists in Haskell is zipWith (+).
Thus we arrive at the following Haskell definition:
pascal 0 = [1]
pascal n = zipWith (+) ([0] ++ pascal (n-1)) (pascal (n-1) ++ [0])
Or in a fashion similar to the famous "lazy fibs":
pascals = [1] : map (\xs -> zipWith (+) ([0] ++ xs) (xs ++ [0])) pascals
Another possible solution (more suitable for beginners in my opinion):
pascal :: Integer -> [Integer]
pascal 0 = [1]
pascal 1 = [1, 1]
pascal n = let p = pascal (n - 1)
in [1] ++ pascalStep p ++ [1]
pascalStep :: [Integer] -> [Integer]
pascalStep [] = []
pascalStep [_] = []
pascalStep (x:y:xs) = x + y : pascalStep (y : xs)
Using let to avoid more space usage.
pascal is calling recursively to find all previous rows, using them to get the next row, until getting to the desired row.
Output:
*Main> pascal 3
[1,3,3,1]
*Main> pascal 4
[1,4,6,4,1]
*Main> pascal 5
[1,5,10,10,5,1]
Start with the base case.
pascal 0 0 = 1
Then handle the edge cases
pascal n 0 = 1
pascal n r | n == r = 1
Now expand with the recursive step
pascal n r = pascal (n - 1) (r - 1) + pascal (n - 1) r
If you want the list for a specific row, write a wrapper
binom n = map (pascal n) [0..n]
Figuring out the types shouldn't be hard
pascal :: Integral a => a -> a -> a
binom :: Integral a => a -> [a]
I'm on my phone so please excuse the mistakes, but you can use Haskell's lazy evaluation in a really cool way here.
pascals :: [[Int]]
pascals = [1]:map (\r -> zipWith (+) (0:r) (r++[0])) pascals
Which you could make point free with a fork but it's rather esoteric.
pascals :: [[Int]]
pascals = [1]:map ((zipWith (+) -<) (0:) (++[0])) pascals
But I personally really like this code, and thinks it's worth being readable-
pascals :: [[Int]]
pascals = [1]:map next pascals
where next = (zipWith (+) -<) (0:) (++[0])
But combinators like that can get a bit confusing, no matter how much I like point free programming.