Hi Everybody,
Please read the section on List Comprehensions
And then read the next sections through "Input and Output" (5 or 6 sections)
or "Functionally Solving Problems"
http://learnyouahaskell.com/starting-out
For Homework #1, solve these problems.
The TA will send out instructions for submission of your code.
Do this as an INDIVIDUAL Project.
You MAY use internet resources, for example, there are standard
solutions for part A and B that you can look up. Using existing
solutions for part A and B is OK. The purpose of this assignment
is for you to solve parts C and D by yourself.
A) Define an infinite list containing the Fibonacci sequence
[1, 1, 2, 3, 5, 8, 13, 21, É]
B) Define an infinite list of prime numbers
[2, 3, 5, 7, 11, 13, 17, É]
C) Define an infinite list that contains the Fibonacci numbers whose
position corresponds to a prime number
[1, 2, 5, 13, É]
D) Define an infinite list that contains the prime numbers whose
position corresponds to a Fibonacci number
[2, 2, 3, 5, 11, É
What I mean by this is that if you assign position numbers
to the items of a list, for example, for Fibonacci we have
position: value
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
...
Then you can select only certain items of the list by position.
For example, part C corresponds to selecting positions that
are prime numbers, which are starred below:
position: value
1: 1
*2: 1
*3: 2
4: 3
*5: 5
6: 8
*7: 13
É
So the result is just the starred items:
[1, 2, 5, 13, É]
Your answer should be a "main" program that prints out
the first 10 elements of each of the lists, for parts A, B, C, D.
main = do
print (take 10 fibs)
print (take 10 primes)
print (take 10 partC)
print (take 10 partD)
You need to define the operations "fibs", "primes", "partC", and
"partD" to compute the required lists. The program should work if you
use a different number besides 10.
Note that you can write a general function to implement the process
of selecting items by position. Then you can use this function for both
parts C and D. Performance does not matter, but you may find that
it can be very slow depending on how you write the code!