I have written the following code to generate a list containing the Fibonacci numbers.
fibonacci = [a + b | a <- 1:fibonacci, b <- 0:1:fibonacci]
I would expect the output of the list to be [1,2,3,5,8,13..], however, the output is not the Fibonacci sequence.
I can't quite understand why it doesn't work.
My reasoning is that, if the Fibonacci numbers are [1,2,3,5,8,13..] then this will be equal to the sum of the 2 lists [1,1,2,3,5,8,13..] and [0,1,1,2,3,5,8,13..], which are equivalent to 1:[1,2,3,5,8,13..] and 0:1:[1,2,3,5,8,13..] or 1:fibonacci and 0:1:fibonacci
I have looked up other ways of implementing this sequence, however I would really like to know why my code doesn't work.
The problem
With:
fibonacci = [a + b | a <- 1:fibonacci, b <- 0:1:fibonacci]
you are generating every possible combinations of the two lists. For example with:
x = [a + b | a <- [1, 2], b <- [3, 4]]
the result will be:
[1 + 3, 1 + 4, 2 + 3, 2 + 4]
Live demo
With zipWith
The closest you can get is with zipWith:
fibonacci :: [Int]
fibonacci = zipWith (+) (1:fibonacci) (0:1:fibonacci)
Live demo
List comprehensions model
Non-determinism
Cartesian products
Nested for-loops
which are all equivalent. So your Fibonacci sequence is wrong because it's computing way too many elements. In pseudocode it's a bit like
fibonacci =
for i in 1:fibonacci:
for j in 0:1:fibonacci:
i + j
What you really want is to zip the lists together, to perform computations in the order of the length of fibonacci instead of its square. To do that we can use zipWith and, with a little algebra, get the standard "tricky fibo"
fibonacci = zipWith (+) (1:fibonacci) (0:1:fibonacci)
fibonacci = zipWith (+) (0:1:fibonacci) (1:fibonacci) -- (+) is commutative
fibonacci = zipWith (+) (0:1:fibonacci) (tail (0:1:fibonacci)) -- def of tail
Then we just define
fibonacci' = 0:1:fibonacci
fibonacci' = 0:1:zipWith (+) (0:1:fibonacci) (tail (0:1:fibonacci))
fibonacci' = 0:1:zipWith (+) fibonacci' (tail fibonacci')
which is the standard with
fibonacci = drop 2 fibonacci'
You can also use the ParallelListComprehension extension which lets you do zipping in list comprehensions with a slightly different syntax
{-# ParallelListComp #-}
fibonacci = [a + b | a <- 1:fibonacci | b <- 0:1:fibonacci]
> take 10 fibonacci
[1,2,3,5,8,13,21,34,55,89]
List comprehensions don't work like that. You've written a nested traversal, whereas what you are trying to do is a zip.
To see the difference, consider:
Prelude> let fibs = [ a + b | (a,b) <- zip (1 : fibs) (0 : 1 : fibs) ]
Prelude> take 10 fibs
[1,2,3,5,8,13,21,34,55,89]
Which works as you'd expect.
There is a syntactic extension to Haskell that allows for parallel comprehensions, so the syntax does a zip for you. You can enable it with -XParallelListComp and then write:
Prelude> let fibs = [ a + b | a <- 1 : fibs | b <- 0 : 1 : fibs ]
Prelude> take 10 fibs
[1,2,3,5,8,13,21,34,55,89]
Another solution without zip or ParallelListComprehension is:
> fib = 0:1:[ last x + head y | x:y:[] <- [ [take i fib, drop i fib] | i <- [1,2..] ] ]
> take 10 fib
[0,1,1,2,3,5,8,13,21,34]
Related
I have been trying to implement a list of Fibonacci number sequence from 0 to n without using the lazy zipwith method. What I have so far is code that returns a list from n to 1. Is there any way I can change this code so it returns the list from 0-n at all?
Example:
fib_seq 4 = [3,2,1,1]
-- output wanted: [1,1,2,3]
If there is not a way to do what I want the code to do, is there a way to just return the list of Fibonacci numbers taking in a number say again 4 it would return [0, 1, 1, 2].
fib_seq :: Int -> [Int]
fib_seq 0 = [0]
fib_seq 1 = [1]
fib_seq n = sum (take 2 (fib_seq (n-1))) : fib_seq (n-1)
Another way you could choose to implement the fib numbers is the use of a helper function then a function on it's own that will produce the infinite list of fib numbers, or you could use take 10 fibs and the output for this would be the first 10 fib numbers. My function is definitely not the fastest way to work out the fib numbers infintely that would be with the zipWith function, but you are not using that here so here is my way to implement it without zipWith.
for example take 10 fibs would return: [0,1,1,2,3,5,8,13,21,34]
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fibs :: [Int]
fibs = (map fib [0..])
It is often the case that you can solve a problem by considering a slightly more general version of it.
Say we want the infinite Fibonacci list starting with two prescribed initial values a and b. There is an obvious recursive solution:
$ ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
...
λ>
λ> aux_fib a b = a : (aux_fib b (a+b))
λ>
λ> take 4 (aux_fib 1 1)
[1,1,2,3]
λ>
And so:
λ>
λ> fib_seq n = take n (aux_fib 1 1)
λ>
λ> fib_seq 4
[1,1,2,3]
λ>
Note: camel case is regarded as more idiomatic in Haskell, so it would be more like auxFib and fibSeq.
If you wanted to have the list start from 0 you could use a helper function and then use this helper function within your fib_seq (which i recommend you change to Camel case so like fibSeq, standard haskell notation)
Ok so the functions as follow fibSeq 7 would return [0,1,1,2,3,5,8]:
fibHelp :: Int -> Int -> [Int]
fibHelp x y = x : (fibHelp y (x+y))
fibSeq :: Int -> [Int]
fibSeq n = take n (fibHelp 0 1)
It feels a bit like cheating, but you could use the closed formula for the Fibonacci sequence like this:
fib n = (phi^n - psi^n) / sqrt 5
where
phi = (1 + sqrt 5) / 2
psi = (1 - sqrt 5) / 2
fibSeq n = fib <$> [1 .. n]
Otherwise the Haskell Wiki has many more implementation variants to chose from. For example very succinctly
fibs = 0 : 1 : next fibs
where
next (a : t#(b:_)) = (a+b) : next t
I already have the following functions
toBin, auxBin :: Integer -> [Integer]
toBin 0 = [0]
toBin n = reverse (auxBin n)
auxBin 0 = []
auxBin n = n `mod` 2 : auxBin (n `div` 2)
fib :: Int -> Integer
fib n = fibs !! n
where
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
fibonacci = [fib n | n <- [0..]]
But when I map the Fibonacci list with the toBin function, I obtained an incorrect list:
Becuase, I obtained this:
[[0],[1],[1],[1,0],[1,1],[1,0,1],[1,0,0,0],[1,1,0,1],[1,0,1,0,1],[1,0,0,0,1,0]]
But, I want this:
[0,1,10,101,1010,10101,101010,1010101,10101010,101010101]
Could you help me?
There are many ways to add digits together in order to get a number. Here's one way. Probably not the most efficient way, but on the other hand built up from smaller functions.
Values like [1,0,1],[1,0,0,0], and [1,1,0,1] are lists in their own right, so what we can do first is to index them. The only trouble is that we'd like to index them in descending order. You can do that by reversing them, indexing them, and then reverse them again, e.g.:
Prelude> reverse $ zip [0..] $ reverse [1,1,0,1]
[(3,1),(2,1),(1,0),(0,1)]
The first element in each tuple is the order of magnitude, so you just need to make it a power of ten:
Prelude> :m +Data.Bifunctor
Prelude Data.Bifunctor> reverse $ fmap (first (10 ^)) $ zip [0..] $ reverse [1,1,0,1]
[(1000,1),(100,1),(10,0),(1,1)]
Now you can simply multiply the elements of the tuples together:
Prelude Data.Bifunctor> reverse $ fmap (uncurry (*) . first (10 ^)) $ zip [0..] $ reverse [1,1,0,1]
[1000,100,0,1]
Finally, you can add all of those numbers together. In fact, you don't need to reverse the reversed list:
Prelude Data.Bifunctor> sum $ fmap (uncurry (*) . first (10 ^)) $ zip [0..] $ reverse [1,1,0,1]
1101
You can put such a combination in a function and map your values over it.
A more efficient solution would probably be to just be to do a left fold (foldl), e.g.:
Prelude> foldl (\acc x -> (10 * acc) + x) 0 [1,1,0,1]
1101
Prelude> foldl (\acc x -> (10 * acc) + x) 0 [1,1,1]
111
An idea could be to represent binary values in decimal notation. So we "transform" 2 into 10.
We can do that by writing a recursive function:
bintodec :: Integral i => i -> i
bintodec 0 = 0
bintodec i = (mod i 2) + 10 * bintodec (div i 2)
This only works for positive values, but that is not really a problem here, since Fibonacci numbers are positive numbers.
Now we already have a definition of the Fibonacci numbers, like in your answer:
fibs :: Num n => [n]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
So the only thing we still have to do is map every elements of fibs with bintodec:
binfibs :: Integral i => [i]
binfibs = map bintodec fibs
The first 15 numbers are then:
Prelude> take 15 binfibs
[0,1,1,10,11,101,1000,1101,10101,100010,110111,1011001,10010000,11101001,101111001]
The nice thing is here that we do not use any binary lists to process it, but keep working in the integer world, which is usually more safe.
Based on the second list however, this has nothing to do with Fibonacci numbers. There you start with 0, and you then shift a zero or a one in at the right end of the number.
We can use iterate for this:
iterate (\x -> 10 * x + 1-(mod x 2)) 0
which produces:
Prelude> take 15 $ iterate (\x -> 10 * x + 1-(mod x 2)) 0
[0,1,10,101,1010,10101,101010,1010101,10101010,101010101,1010101010,10101010101,101010101010,1010101010101,10101010101010]
This question already has answers here:
Understanding Haskell's fibonacci
(9 answers)
Closed 5 years ago.
this one:
fib = 1 : 1 : [a + b | (a, b) <- zip fib (tail fib)]
i understand that at first fib is [1, 1, ..]
(.. = rest of the list)
so here fib = [1, 1, ..] and tail fib = [1, ..] so (a, b) is (1, 1) which translate to a + b which is 2 and now the list looks like that:
fib = 1 : 1 : 2 : [a + b | (a, b) <- zip fib (tail fib)]
and here i get confused:
now i think fib is [1, 1, 2, ..] and tail fib is [1, 2, ..]
and if i zip the lists, i again should get that a + b is 1 + 1 because those are still the first elements of the two lists.
where am i wrong here?
(i know i am but i can't understand why and what is going on here)
You are correct that
fib = 1 : 1 : [a + b | (a, b) <- zip fib (tail fib)]
produces the next element to be 2. However, fib = 1 : 1 : 2 : [a + b | (a, b) <- zip fib (tail fib)] is not accurate for the next step. Since you evaluate zip fib (tail fib) on this step, it must be substituted from its definition, not left as it is. A more accurate formula looks something like:
fib = 1 : 1 : 2 : [a + b | (a, b) <- zip (tail fib) (tail (tail fib)]
One way to think of the evaluation of zip is that each application consumes the next two head elements of each of the given lists. In this case, the next iteration will return (a, b) = (1, 2) since this is the next pair of numbers in fib and (tail fib) respectively.
In a similar fashion as the Fibonacci series may be generated as follows,
fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
how to define the series for factorial.
Update
Embarrassingly enough, tried this quite before adding this question,
Prelude> let factorial = 2 : 6 : zipWith(*) factorial (tail factorial)
Prelude> take 5 factorial
[2,6,12,72,864]
Indeed the numbers in the tail are not successive values, to start with.
Lets take a step back and remember where that lazy version actually comes from:
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
We can also define the factorial similarly:
factorial 0 = 1
factorial n = factorial (n - 1) * n
As you can see, our zipping operation is actually (*), and the second list won't be a sublist of factorials, but instead [x..] with an appropriate x:
factorials = 1 : zipWith (*) factorials [x..]
What value should x be? Well, the second element should be 1 = 1 * 1, so it's 1, naturally:
factorials = 1 : zipWith (*) factorials [1..]
Note that we only need to give the first element, since we don't use tail or something similar. As you can see, your attempt was almost correct. You just used the wrong values for the left hand side:
Prelude> let factorial = 2 : 6 : zipWith (*) [4..] (tail factorial)
Prelude> take 10 $ factorial
[2,6,24,120,720,5040,40320,362880,3628800,39916800]
Remark: The factorial sequence is 0!, 1!, 2!, ..., so if you want to be OEIS compliant start with [1,1,...].
The idiomatic definition of a lazy list of factorials is not recursive at all: instead it uses the Prelude function scanl.
factorials = scanl (*) 1 [1..]
Given the usual definition of factorial:
factorial :: Integer -> Integer
factorial 0 = 1
factorial i = foldr (*) 1 [2..i]
we can generate an infinite list of all factorials by simply running the factorial function over an infinite list of all positive numbers:
inFact :: [Integer]
inFact = map factorial [0..]
Live demo
I've been trying to do an infinite list of powers the same as I done them like below, for a list of fibonacci numbers and factorials.
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
facs = 1 : zipWith (*) [1 ..] facs
Thanks
Generating the powers of a number is as simple as using 'iterate':
iterate (*2) 1
To find a specific power (instead of listing them) it is faster to use (^). To see the individual steps of a large multiplication you can use scanl:
scanl (*) 1 [2, 3, 5, 7]
Finally to generate a list of all squares this is the approach I recommend:
fix (\more r s -> s : more (r + 1) (s + 2*r + 1)) 0 0
Or if you are uncomfortable with fix here are two alternative versions:
unfoldr (\(r, s) -> Just (s, (r + 1, s + 2*r + 1))) (0, 0)
map snd . iterate (\(r, s) -> (r + 1, s + 2*r + 1)) $ (0, 0)
For greater readability you can also use map:
List of consecutive powers of 2:
λ> map (2^) [0..10]
[1,2,4,8,16,32,64,128,256,512,1024]
Consecutive squares:
λ> map (^2) [0..10]
[0,1,4,9,16,25,36,49,64,81,100]
I think you can define an infinite sequence of the squares with just a list comprehension:
powers = [ ii*ii | ii <- [1 ..]]
take 10 powers
=> [1,4,9,16,25,36,49,64,81,100]
Edit: It's been explained that you are after the powers of 2, which can also be done with a list comprehension:
powersOf2 = [ 2^ii | ii <- [0 ..]]
take 10 powersOf2
=> [1,2,4,8,16,32,64,128,256,512]
You can extrapolate this to a generator function for any given base:
powersOfN nn = [ nn^ii | ii <- [0 ..]]
take 10 (powersOfN 3)
=> [1,3,9,27,81,243,729,2187,6561,19683]
take 10 (powersOfN 17)
=> [1,17,289,4913,83521,1419857,24137569,410338673,6975757441,118587876497]
powers n = 1 : map (n*) (powers n)
If you want an infinite list of power N by zipWith, this may be an easy way:
powersOfN :: [Integer]
powersOfN
= zipWith (^) [N,N..] [0,1..]
Here is an example of the power of 2:
Prelude> zipWith (^) [2,2..] [0,1..]
=>[1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728..]