Dividing lists??? 
Author Message
 Dividing lists???

We're two amateurs in Prolog-programming and we have the following
problem:
To write a program that divides a list (entered by the user) into
two lists. The first list has to consist of two elements. The second
should consist the rest of the list.
Urgent need for answer!
Yours sincerely



Thu, 25 Jun 1998 03:00:00 GMT  
 Dividing lists???

Quote:

>We're two amateurs in Prolog-programming and we have the following
>problem:
>To write a program that divides a list (entered by the user) into
>two lists. The first list has to consist of two elements. The second
>should consist the rest of the list.
>Urgent need for answer!
>Yours sincerely


firstTwo([A,B|Rest],[A,B],Rest).


Tue, 30 Jun 1998 03:00:00 GMT  
 Dividing lists???

Quote:

>We're two amateurs in Prolog-programming and we have the following
>problem:
>To write a program that divides a list (entered by the user) into
>two lists. The first list has to consist of two elements. The second
>should consist the rest of the list.
>Urgent need for answer!
>Yours sincerely


how about this:

% call this with:
% div_list(List, First, Rest)

% empty input
div_list([], [],[]).

%only one element
div_list([A], [A], []).

%at least 2 elements
div_list([A, B], C, [ A, B | C]).

nice & easy, isn't it ?

--
============================================================================
             CS Student                                   | I may be late,
Real       : Frank Moehle, 26121 Oldenburg, Germany       | but I'm worth



----------------------------------------------------------------------------
****              It`s how you DO it, not how you don't.                ****
----------------------------------------------------------------------------
                         Your DOS is my SWAPspace



Fri, 03 Jul 1998 03:00:00 GMT  
 Dividing lists???
Quote:

> Re: Dividing lists???


> 12 Jan 1996 12:24:19 GMT
> Department of Software Development, Monash University
> Newsgroups:
> comp.lang.prolog
> References:


> >We're two amateurs in Prolog-programming and we have the following
> >problem:
> >To write a program that divides a list (entered by the user) into
> >two lists. The first list has to consist of two elements. The second
> >should consist the rest of the list.
> >Urgent need for answer!
> >Yours sincerely


   > firstTwo([A,B|Rest],[A,B],Rest).

   I would write (in PDC Prolog) the following

   two_rest([], [], []) :- !.   % source is empty
   two_rest([X}, [X], []) :- !. % source with the single element
   two_rest([X,Y|T], {X,Y], T).



Sat, 04 Jul 1998 03:00:00 GMT  
 Dividing lists???
Homework problem:

Quote:
>> >To write a program that divides a list (entered by the user) into
>> >two lists. The first list has to consist of two elements. The second
>> >should consist the rest of the list.

Proposed answer:

Quote:
>   two_rest([], [], []) :- !.   % source is empty
>   two_rest([X}, [X], []) :- !. % source with the single element
>   two_rest([X,Y|T], {X,Y], T).

This is surely inappropriate on two counts.

(1) The request clearly stated that "The first list ***HAS*** to consist
    of two elements."  The code provided can return a list with no elements
    (wrong), one element (wrong), or two elements (with a left brace {
    instead of a left bracket [).

    The request did not state what was to be done with user-provided lists
    having fewer than two elements.  It is always inappropriate to make
    things up and more so when they violate a constraint that is explicitly
    stated.

(2) The request clearly state that the list to be divided is to be
    "(entered by the user)".  This sounds very much as though input/output
    is required.  The request did not say that the user is to enter a
    _query_, but a _list_.

A program that appears to meet the requirements is

    main :-
        write('Enter a list in Prolog syntax: '),
        read(Input),
        (   Input = [X1,X2|Xs] ->
            writeq([X1,X2]), put("."), nl,
            writeq(Xs), put("."), nl
        ;
            write('Input is not a list of at least two elements.'), nl
        ).

Now a _predicate_ that can divide a list into two parts is easy:

        :- [library(length)].   % In Quintus Prolog
        p(Input, Two, Rest) :-
            append_length(Two, Rest, Input, 2).

(:-) (:-) (:-) but true anyway.
--
"conventional orthography is ... a near optimal system for the
 lexical representation of English words." Chomsky & Halle, S.P.E.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.



Mon, 06 Jul 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Dividing a list into sublists of n elements

2. Dividing a list into two?

3. Divide a list

4. Intersection of multiple lists/list of lists

5. DXOracle / PyADO - convert lists of tuples to list of lists

6. Timing for matrix divide

7. D5: ProportionalLayout divide by zero

8. 64-bit divide.

9. zero divide by zero (2)

10. 0 divided by 0 in APL and J References

11. zero divided by zero

12. DIVIDE BY ZERO in APL2 V1.02 under WinNT 4.0

 

 
Powered by phpBB® Forum Software