This part of your code is a Haskell range
[test n .. test (n-1)]
Ranges work by figuring out the left number and the right number, and then constructing a list that contains all steps from the left number to the right number. So:
[1 .. 6] --> [1,2,3,4,5,6]
[5 .. 9] --> [5,6,7,8,9]
As you can see, the default step is 1, so if you have a left number that is higher than the right, you will get an empty list:
[4 .. 3] --> []
As an aside, You can override the default step by providing another number:
[1, 3 .. 6] --> [1,3,5] -- step is 2
[8, 6 .. 3] --> [8,6,4] -- step is -2
As you can see, when you have another step size than 1, you have to be careful with what gets included in the resulting list. This goes especially for negative steps, and even more if you have non-integer steps like [1, 1.25, .. 2.1]. You should almost never generate a list of non-integer numbers using a range.
In your solution you have the line
test n = sum[test n .. test (n-1)]
According to the rules for ranges, this is bound to go wrong. When the program tries to make the list from the range, it tries to compute test n since that is the left number of the range. But that gets us nowhere, since test n is what this whole line is trying to compute in the first place. So we have an infinite loop, and the program hangs.
You could try to do
test n = sum[1 .. test (n-1)]
That looks closer to the examples you gave. It starts with 1 (which is test 1), and ends with test (n-1). But the problem is those values in between. Because ranges have the step of one, what you end up with is:
[1 .. test (n-1)] --> [1,2,3, ......., test (n-1)]
which is not the same as
[test 1, test 2, test 3, .... , test (n-1)]
And since a range can only have a constant step, there is no way to get this last line with a simple range, even if you override the default step. One hint on how to solve this is to notice the number of elements in the list.
length [1 .. test (n-1)] --> test (n-1),
-- because [1,2,3] has 3 elements, [1,2,3,4] has 4 and so on
length [test 1, test 2, test 3, ....... , test (n-1)] --> n-1
-- this is not quite Haskell syntax
The Haskell way here is to make a list that has the correct number of elements, and then transform it so each element is the correct one. How do you make a list of (n-1) elements? Simple:
[1..(n-1)]
From here you can go several ways. There is the list comprehension from luqui:
[test x | x <- [1..(n-1)]]
You can think of this as taking each number out of the range, assigning it to x and then applying the test function to x, so you get [test 1, test 2, test 3, ....... , test (n-1)]. Another way would be to use the map function:
map test [1..(n-1)]
I think of this as applying test to each element of the list at the same time, but it is exactly the same thing as the list comprehension, just two ways of looking at it. Notice that both ways use the [1..(n-1)] range.
If you use either of these instead of the [test n .. test (n-1)] range in your original code, you are very close to the solution. The only thing missing, as luqui reminds, is to remember to add the 1.
problem2implemented, you've shown us onlytest?problem2?x_i = 2 ^ (i-1), no recursion is needed.[test 1 .. test n]does not mean[test 1, test 2, test 3, and so on up to test n]! To see what's going on, try it out with a non-recursive function likesquare x = x * xand see what you get for[square 1 .. square 5]. Then try[ square i | i <- [1..5] ]and/ormap square [1..5].