Unpredictable dynamic predicate behaviour - list

I have a problem that requires me to add elements to a list that are spread across various predicates. Rather than doing via argument based lists I've opted to use a dynamic list predicate. I simple example can be seen below. When I initially used it it worked fine but now if I use any argument as X it keeps retrieving previous argument data from the list even after closing the program and recompilation. Does anybody know what's wrong with it?
//list declarations
:- dynamic listD/1.
listD([]).
//pushes X onto the list then retrieves the entire list for verification
sample(X):-
assert(listD(X)),
listD(Y),
write(Y).
Example usage
sample([adam]).
//prints adam fine
sample([fred]).
//prints adam again

Use retractall to clean up the state when you start.
sample(X):-
retractall(listD(_)),
assert(listD(X)),
listD(Y),
write(Y).

Related

How to create a function that returns a list that is a union of the elements of a nested list in Ocaml?

If I was given a set of lists within a list in Ocaml, for example [[3;1;3]; [4]; [1;2;3]], then how can we implement a function to return a list that is a union of the values of the nested list (so the output from the example will return [1;2;3;4])? I tried removing duplicates from the list, but it didn't work as intended. I am also restricted to using the List module only.
Restricted to using the List module only? Sounds like homework with an arbitrary limit like that. so I don't want to give a fully working solution. However, if you look through the List documentation, you'll see a couple of functions that can be combined to do what you want.
concat, which takes a list of lists and flattens them out into a single list, and sort_uniq, which sorts a list and removes duplicates.
So you just have to take your list of lists, turn it into a single list, and sort_uniq that (With an appropriate comparison function) to get your desired results.

Groovy: Iterating a list

I was trying to iterate a list and after googling, I found one solution like below:
value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])
I did not understand this part:
sort{new Random()}
Can someone explain this?
And which class the take method is belonging to?
sort receives a closure to determine the order that you wish. In this example, it makes a random sort.
take comes from Iterable and returns the first n elements.

It is possible to select one attribute from all elements in a list?

I'm trying to make a tracking algorithm, for this I need to constantly get the position of all the elements in the list, I want to know if there is a way to do it in a single line without a "ForEach", I know you can copy one whole list to another list, that is what I am doing right now.
I have this:
List copy = List original;
But I want to do something like this
List copy = List original.Position
Note: position is a vector2 var inside my list.
I would recommend using Linq:
List<Position> copy = original.Select(e => e.Position).ToList();
'e' I randomly chose, e stands for element. It represents each element in the list. This is the equivalent to:
List<Position> copy = new List<Position>();
foreach(Element e in original)
{
copy.Add(e.Position);
}
This will return a List<Position>. Be aware that this List won't be of the same type as the original.
Also note that Linq queries, whilst clean, concise and easy to read, aren't as efficient as manually looping. However for a small list, a couple of hundred or less, you won't see any difference.
Here is a link to a very in depth SO question on this topic: foreach + break vs linq FirstOrDefault performance difference
And here is a link to using linq queries: http://msdn.microsoft.com/en-us/library/bb397906.aspx
In the example in the last link (to MSDN) you will see two forms of linq notation. One is the standard notation as I have used. The other is query form, which is similar to calling Where() using the standard notation. I find the query notation similar to writing SQL queries.
One final note is that you can also produce an array using ToArray() instead of ToList(). Preferable if the copy will be of a fixed size and you are going to randomly access them. Regardless of which you use, you can rely on the order of the copy to be the same as the original and that's how you should relate it to the original, so the 4th Position in copy is the Position of the 4th element in original for example.
I hope this has helped.

Does appending a list to another list in F# incur copying of underlying objects or just the pointers?

I've always thought that appending a list to another one meant copying the objects from the first list and then pointing to the appended list as described for example here.
However, in this blog post and in its comment, it says that it is only the pointers that are copied and not the underlying objects.
So what is correct?
Drawing from Snowbear's answer, a more accurate image of combining two lists (than the one presented in the first referred article in the question) would be as shown below.
let FIRST = [1;2;3]
let SECOND = [4;5;6]
let COMBINED = FIRST # SECOND
In the functional world, lists are immutable. This means that node sharing is possible because the original lists will never change. Because the first list ends with the empty list, its nodes must be copied in order to point its last node to the second list.
If you mean this statement then the answer is seems to be pretty simple. Author of the first article is talking about list node elements when he says nodes. Node element is not the same as the list item itself. Take a look at the pictures in the first article. There are arrows going from every element to the next node. These arrows are pointers. But integer type (which is put into the list) has no such pointers. There is probably some list node type which wraps those integers and stores the pointers. When author says that nodes must be copies he is talking about these wrappers being copied. The underlying objects (if they were not value types as in this case) would not be cloned, new wrappers will point to the same object as before.
F# lists hold references (not to be confused with F#'s ref) to their elements; list operations copy those references (pointers), but not the elements themselves.
There are two ways you might append items to an existing list, which is why there seems to be a discrepancy between the articles (though they both look to be correct):
Cons operator (::): The cons operator prepends a single item to an F# list, producing a new list. It's very fast (O(1)), since it only needs to call a very simple constructor to produce the new list.
Append operator (#): The append operator appends two F# lists together, producing a new list. It's not as fast (O(n)) because in order for the elements of the combined list to be ordered correctly, it needs to traverse the entire list on the left-hand-side of the operator (so copying can start at the first element of that list). You'll still see this used in production if the list on the left-hand-side is known to be very small, but in general you'll get much better performance from using ::.

Finding a path between two lists

I've been tasked with the portion of the code that reads through two lists and finds a path between the two lists.
My problem is however with reading and returning a TRUE case if I find the required node in the first list. I've been writing my code as below.
%declare the list
circle_line([cc1,cc2,cc3,cc4,cc5,cc6,cc7,cc8,cc9,cc10,cc11,cc12,cc13,cc14,cc15,cc16]).
%the predicate that finds the station i need
check_for_station(A) :- circle_line(X),
member(A,X),
write(A),nl.
Then in the cosole I type: check_for_station(cc9).
But the answer I get is "no."
I have a feeling that I'm declaring the list wrong, because in the debugger the value of X comes out to be "H135" or something and it clearly does not loop through every element to find the one that I need.
As whythehack found, in Amzi! Prolog the classic (and nondeterministic) member/2 predicate is not a "built-in" but is provided (along with other useful list manipulation predicates like length/2) in the list library. See here for some context about the built-in versus library predicates for lists.
If check_for_station/1 is only to be called with its argument A bound (as the example suggests), then Amzi!'s built-in (and deterministic) predicate is_member/2 will do the job (and a bit more quickly).
The thinking seems to be that nondeterministic member/2 is a two-line user-definition, and providing a faster deterministic version (not permitting backtracking over members of a list) is something the user could not easily provide for themselves.