Your solution explained: If we reverse the empty list, we obtain the empty list. If we reverse the list [H|T] , we end up with the list obtained by reversing T and concatenating with [H] . To see that the recursive clause is correct, consider the list [a,b,c,d] . If we reverse the tail of this list we obtain [d,c,b] . Concatenating this with [a] yields [d,c,b,a] , which is the reverse of [a,b,c,d]
Another reverse solution:
reverse([],Z,Z). reverse([H|T],Z,Acc) :- reverse(T,Z,[H|Acc]).
call:
?- reverse([a,b,c],X,[]).
For further information please read: http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse25
Related Posts:
- How do I append lists in Prolog?
- List Length in Prolog
- How do I concatenate two lists in Python?
- Print a list in reverse order with range()?
- How do I concatenate two lists in Python?
- How do I concatenate two lists in Python?
- Inorder Binary Tree Traversal (using Python)
- How to have a set of sets in Python?
- How to concatenate items in a list to a single string?
- Learning Haskell: How to remove an item from a List in Haskell
- How to obtain a subarray in python 3
- creating a reverse method for a python list from scratch
- Editing a Post, 99% CPU?
- Similar posts formatting
- Introductory paragraph only in list of blog posts
- List all categories in options
- WooCommerce Grid / List view
- custom walker wp menu last element
- How to divide and display categories into two columns
- To get list of bottom most or deepest or last child for specified parent category
- List first letter of categories – issue upon spcecial characters (danish letters)
- How do I create an alphabetically organized glossary of links?
- Generate a nested list of all posts, sorted first by year, then by month
- adding a #hash to each link in list_pages()?
- How correct list-style displayed in firefox? [closed]
- Output categories in something different than a list
- Wrap Meta Box with and for each new line
- get_all_category_ids for bookmark
- Add thumbnails in ‘li’ list
- List child pages alphabetically
- How to make a text string into a bullet list [closed]
- How do I use ‘git reset –hard HEAD’ to revert to a previous commit? [duplicate]
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Understanding slice notation
- TypeError: list indices must be integers or slices, not str
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- Difference between del, remove, and pop on lists
- How to overcome TypeError: unhashable type: ‘list’
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Finding the index of an item in a list
- Difference between Arrays.asList(array) and new ArrayList
(Arrays.asList(array)) - Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- How to make a flat list out of a list of lists
- Convert list to array in Java [duplicate]
- Removing duplicates in lists
- TypeError: list indices must be integers or slices, not list
- Accessing the index in ‘for’ loops?
- What does the list() function do in Python?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- Converting array to list in Java
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- How to get the list of all installed color schemes in Vim?
- TypeError: ‘float’ object is not subscriptable
- String concatenation: concat() vs “+” operator
- Transpose/Unzip Function (inverse of zip)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Converting array to list in Java
- Check if something is (not) in a list in Python
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Finding the average of a list
- How to concatenate a std::string and an int
- Writing a list to a file with Python
- if else in a list comprehension
- How can I randomly select an item from a list?
- How do you reverse a string in-place in JavaScript?
- Convert Set to List without creating new List
- Get unique values from a list in python [duplicate]
- Fastest way to check if a value exists in a list
- How to Sort a List
by a property in the object - How can I count the occurrences of a list item?
- TypeError: ‘list’ object cannot be interpreted as an integer
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- How to find all occurrences of an element in a list
- Get list from pandas dataframe column or row?
- Finding and replacing elements in a list
- How to find all the indexes of a recurring item in a list?
- How to reverse an std::string? [duplicate]
- ValueError: max() arg is an empty sequence
- Python list sort in descending order
- Remove list from list in Python
- python – if not in list
- Concatenating string and integer in python