Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# prolog splitting a list into 3 other lists and each have different type like + and -

Posted on 2005-03-03
Medium Priority
364 Views
Hi, i am trying to split a list into 3 lists: the first one consists of only positive numbers and the second one of negative numbers and the third one of atoms only for example:

?- partition([5,1,anything,-2,-3,10], L1, L2, L3).

L1= [5,1,10]
L2=[-2,-3]
L3=[anything]

any ideas????
0
• 2

LVL 25

Expert Comment

ID: 13451766
Could you elaborate.. some more examples?
What language are you using? Where is this data stored? In a file??

>> IM
0

Author Comment

ID: 13451869
this is prolog language.

?- partition([5,1,anything,-2,-3,10], L1, L2, L3). --> this is the question that the user should ask

the program should give him the following answer:

L1= [5,1,10]
L2=[-2,-3]
L3=[anything]

another example:
?- partition([5,1,anything,-2,whatever,10], L1, L2, L3).

L1= [5,1,10]
L2=[-2]
L3=[anything,whatever]

by the way nothing should be stored in a file. it's only a simple interaction with the user.
the user will give the words including positive and negative numbers and we have to differenciate between them and put them in lists L1, L2, L3

0

LVL 9

Accepted Solution

gabeso earned 1500 total points
ID: 13457019
You would want something like this:

partition( [H | T], [ H | L1 ], L2, L3 ) :- H > 0.
partition( [H | T], L1,[ H |  L2], L3 ) :- H <= 0.
partition( [H | T], L1, L2, [H | L3] ) :- if H is something else ....

0

Author Comment

ID: 13462078
did you try this to see if it's working?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Progress
Loops Section Overview
###### Suggested Courses
Course of the Month11 days, left to enroll