1

I'm trying to get a list comprehension working, which intention is to verify that each element X in List is followed by X+Incr (or an empty list). Later, I shall use that list and compare it with a list generated with lists:seq(From,To,Incr). The purpose is to practice writing test cases and finding test properties.

I've done the following steps:

1> List.
[1,3,5,8,9,11,13]
2> Incr.
2
3> List2=[X || X <- List, (tl(List) == []) orelse (hd(tl(List)) == X + Incr)].
[1]

To me, it seem that my list comprehension only takes the first element in List, running that through the filter/guards, and stops, but it should do the same for EACH element in List, right?

I would like line 3 returning a list, looking like: [1,2,9,11,13].

Any ideas of how to modify current comprehension, or change my approach totally?

PS. I'm using eqc-quickcheck, distributed via Quviq's webpage, if that might change how to solve this.

4 Answers 4

2

The problem with your list comprehension is that List always refers to the entire list. Thus this condition allows only those X that are equal to the second element of List minus Incr:

(hd(tl(List)) == X + Incr)

The second element is always 3, so this condition only holds for X = 1.

A list comprehension cannot "look ahead" to other list elements, so this should probably be written as a recursive function:

check_incr([], _Incr) ->
    true;
check_incr([_], _Incr) ->
    true;
check_incr([A, B | Rest], Incr) ->
    A + Incr == B andalso check_incr([B | Rest], Incr).
2
  • Yeah... Of course. Stupid me (; I guess a comprehension isn't the right tool, like André Laszlo said... Feb 23, 2014 at 15:28
  • I will implement a helper function instead, like above comment mentions. Suppose that will be the easiest way! Feb 23, 2014 at 15:30
1

Maybe I'm misunderstanding you, but a list comprehension is supposed to be "creating a list based on existing lists". Here's one way to generate your list using a list comprehension without using lists:seq:

> Start = 1, Inc = 2, N = 6.
6
> [Start + X*Inc || X <- lists:seq(0,N)].
[1,3,5,7,9,11,13]
3
  • Yeah, generating the list isn't the problem. The problem is to check whether List fulfils the criteria that for each element X, the next element is X+Incr or an empty list. Feb 21, 2014 at 14:20
  • Then maybe a list comprehension is the wrong tool for the job, since you're unable to look at neighboring elements? Feb 21, 2014 at 14:21
  • I think you're right... Comprehension is not the right tool! Feb 23, 2014 at 15:31
0

You could do something like this:

> lists:zipwith(fun (X, Y) -> Y - X end, [0 | List], List ++ [0]).
[1,2,2,2,2,2,2,-13]

Then check that all elements are equal to Incr, except the first that should be equal to From and the last that should be greater or equal than -To.

0

One quick comment is that the value List does NOT change when in the comprehension is evaluated, it always refers to the initial list. It is X which steps over all the elements in the list. This means that your tests will always refer to the first elements of the list. As a list comprehension gives you element of a list at a time it is generally not a good tool to use when you want to compare elements in the list.

There is no way with a list comprehension to look at successive sublists which is what you would need (like MAPLIST in Common Lisp).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.