×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

reverse_list

reverse_list

reverse_list

(OP)
Hi, I don't understand how this programm works.
 
reverse_list([], []).
reverse_list([Head | Tail], ReversedList) :-
reverse_list(Tail, ReversedTail),
append(ReversedTail, [Head], ReversedList).

It is assumed to reverse a list, but I don't know what calls does it make and with what order. Can anyone help me? I tried to run it on Swi-Prolog, but it didn' t help me. Thank you.

RE: reverse_list

I suggest you try debugging it in SWI-Prolog to see what happens. Type:

?- guitracer.

at the Prolog prompt. Then type this:

?- trace, reverse_list([5]).
?- trace, reverse_list([5, 6]).
?- trace, reverse_list([5, 6, 7]).

If you understand reversal of 1-element lists, it would be easier to understand reversal of 2-element lists, then 3-element lists and then N-element lists. It's recursivity.

In case you don't have experience with debugging, the green line shows where execution is, and somewhere in the upper left part of the debug window you'll see all the variables involved. To move one step forward with the execution, press SPACE and watch closely how variables are affected.

RE: reverse_list

(OP)
Thank you.

RE: reverse_list

(OP)
When I put trace, reverse_list([5]). ,it says undefined reverse_list/1, however there is a definition for reverse_list/2.

RE: reverse_list

Right, it's my fault ...

Type these instead:

?- trace, reverse_list([5], Result).
?- trace, reverse_list([5, 6], Result).
?- trace, reverse_list([5, 6, 7], Result).

RE: reverse_list

(OP)
Ok, thank you.:)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close