Help with lists.



Greetings,

I have just started learning prolog. I come from a C/C++/Java
background and I am having a very hard time getting my head around this
style of programming.

Can some one please offer me some assistance in creating the following
relationships:

1) A relation flatten( X, Y ) that holds when Y is a list structure
that contains the same elements as X, in the same order, but with no
nested lists beyond the first level. In other words, Y may contain
either atomic elements or other lists, but these may not contain any
nested lists.

Eg:

|?- flatten1( [a, b, [c,d], [[e]]], Y).
Y = [ a, b, [c, d], [e]].
no

|?- flatten1( [a,[b],[[[c]], [d,e]]], X).
X = [a,[b],[c,d,e]].
no

2) A predicate no-triplicates(List1, List2) that replaces all
occurrences of any element that appears three or more times in List1
(i.e., triplicate members) with a single occurrence of that element to
generate List2. This single occurrence must occupy the position in the
list held by the first occurrence of the triplicate member in List1.
Duplicate occurrences are left unchanged. For instance, if the input
list List1 is [1, 2, 3, 2, 3, 3, 4], then the output list List2 must be
[1, 2, 3, 2, 4].

Thanks in advance,

joo.

.



Relevant Pages

  • Re: Thinking outside the box on file systems
    ... While i highly support innovation, until i see a well layed out ... structure of what exactly you are looking for i have a hard time ... Cross the two lists showing how your idea would fix the current problems. ... So I installed Linux" ...
    (Linux-Kernel)
  • Re: Favorite all-time tenors (15)
    ... What do you not like about de Lucia? ... the lists themselves are meaningless without the poster ... I'd have a hard time leaving either one of them off a "favorites" ... Other singers who would certainly make any short list I could make ...
    (rec.music.opera)
  • Re: Favorite all-time tenors (15)
    ... the lists themselves are meaningless without the poster ... beautiful or more so, ... I'd have a hard time leaving either one of them off a "favorites" ... Other singers who would certainly make any short list I could make ...
    (rec.music.opera)
  • Re: [SLE] CCing to the list
    ... >> give people a hard time about something basically irrelevant like ... >> know how they should behave, but don't take it to heart. ... > support list, that due to a general lack of English lists, this one was ...
    (SuSE)
  • Re: If youre good with arrays and lists, this ones for you
    ... Let's say I have two scalars, $list1 and $list2. ... Both are comma separated lists of ordered, unsigned, unique integers. ... We use these techniques rather than the much easier to code in_array ...
    (comp.lang.php)