how to get rid of the unassigned pointer in this case? [closed] - recursion

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here are the facts I use :
edge(route(fra), route(tog), walk).
edge(route(fra), route(105), walk).
edge(route(tog), route(togc), bus).
edge(route(togc), route(sub), walk).
edge(route(sub), route(ag), metro).
edge(route(ag), route(n), metro).
edge(route(n), route(a), metro).
edge(route(a), route(r), metro).
edge(route(re), route(m), metro).
edge(route(m), route(v), walk).
edge(route(105), route(t), bus).
edge(route(t), route(vi), metro).
edge(route(vi), route(m), metro).
edge(route(t), route(3), walk).
edge(route(3), route(m), bus).
And the predicates :
seek(route(v), _).
seek(route(R1), [[R2, How]|Res]) :-
edge(route(R1), route(R2), How),
seek(route(R2),Res).
when I ask the query ?- seek(route(fra), X).
It should return R2 and How in a list, but Res in this case remains unassigned... I don't know how can I get around that to make that query only return a list of lists containing R2. Any help will be appreciated!

The recursion anchor (the first clause of your seek/2 predicate) always returns a variable as second argument. Instead, it should return an empty list.

Related

Prolog, produce lists with m first members reversed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a trouble finding a solution to the following problem in Prolog.
Say we have a list with N+1 members (i.e. [X1, X2, X3, ..., XN, X(N+1)]). We want to write a predicate (with recursion) in prolog that produces all possible lists which have reversed the first m of their members, with m taking values 2<=m<=N. For example if we have the list [1,2,3,4,5,6], then the predicate should return the lists:
[2,1,3,4,5,6], [3,2,1,4,5,6], [4,3,2,1,5,6], [5,4,3,2,1,6].
The predicate must have the form
move(List1, List2):-
Where List1 is the original list and List2 is the resulted list after the reversal of m first members.
Any help is highly appreciated.
Thanks in advance
Shouldn't be more difficult than
reverse_n(Xs,N,Ys) :- % reverse the first N of Xs to form Ys:
length(Pfx,N), % - construct a list of unbound vars of length N
append(Pfx,Sfx,Xs), % - partition the source list into a Pfx and Sfx
reverse(Pfx,Rev), % - reverse the prefix
append(Rev,Sfx,Ys). % - Finally, glue it back together

A simple Common-Lisp cat [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How do I translate this into Common Lisp?
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
I looked in ANSI Common Lisp and duckduckwent a bit, but
couldn't find an answer.
A possibile equivalent is:
(loop for c = (read-char t nil)
while c
do (write-char c))
See the example.

Two method arguments or a new class.. for (X and Y cords) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In the book 'Clean Code' by Robert C. Martin, he recommends making use of:
Circle makeCircle(Point center, double radius);
over
Circle makeCircle(double x, double y, double radius);
He basically argues that making a class to avoid using multiple method arguments is preferred.
What is your opinions on this? Please explain to me the benefits, or disadvantages of either.
There are a few reasons for this.
First, it helps to group parameters meaningfully. In this trivial example, it's immediately obvious that x and y go together, but it might not be as immediately obvious when dealing with a more obscure example.
Perhaps more importantly, it cuts down on having too many parameters to keep track of meaningfully; once you have a method that takes more than 3 or 4 parameters, it gets more and more cumbersome to keep track of which parameter is which. Binding parameters together in a class or struct helps avoid that.
Consider this example:
int HowManyMinutesToAirport(int AirportIdentifier, string AirportName, int PlainIdentifier, string PlainName, int PlaneSpeed, string PlaneCompassDirection);
vs.
int HowManyMinutesToAirport(Airport airport, Plane plane);

(F# RProvider) mean giving a weird result - in some cases [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am starting to use the RProvider. For starters, I have just tried to evaluate functions in different ways. It seems I have already run into problems (perhaps a problem with my understanding of how the RProvider works). I have run the same function in four different ways, which I thought to be equivalent. However, the four example provides me with two different results.
R.sapply(R.c(1,2,3,4,5), R.eval(R.parse(text="mean"))).GetValue<float[]>()
// val it : float [] = [|1.0; 2.0; 3.0; 4.0; 5.0|]
R.sapply(R.c(1,2,3,4,5),"mean").GetValue<float[]>()
// val it : float [] = [|1.0; 2.0; 3.0; 4.0; 5.0|]
R.mean(R.c(1,2,3,4,5)).GetValue<float[]>()
// val it : float [] = [|3.0|]
R.eval(R.parse(text="mean(c(1,2,3,4,5))")).GetValue<float[]>()
// val it : float [] = [|3.0|]
Can anyone tell me why this is? My own guess is that R.sapply applies the given function element-wise. But how do I get around this?
do.call() is the function in R for "applying" a function to a list of parameters (a slightly different meaning from applying or mapping a function over a vector or list of values, which is what the *apply family does).
The R function for what you want would be
do.call("mean",list(c(1,2,3,4,5)))
According to the comments (I don't speak F# myself), the F# analogue would be:
R.do_call("mean", R.list(R.c(1,2,3,4,5)))

Why do Clojure vector function results exclude the stop value? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Forgive me for the newb question and potentially incorrect terminology.
Clojure vector functions produce values that do not include the stop value. For example:
=> (subvec [:peanut :butter :and :jelly] 1 3)
[:butter :and]
=> (range 1 5)
(1 2 3 4)
The doc for range explicitly states this but doesn't give a rational: "...Returns a lazy seq of nums from start (inclusive) to end (exclusive)...".
In Ruby these operations are inclusive:
(1..5).to_a
=> [1, 2, 3, 4, 5]
[:peanut, :butter, :and, :jelly][1,3]
=> [:butter, :and, :jelly]
Obviously these are very different languages, but I'm wondering if there was some underlying reason, beyond a personal preference by the language designers?
Making the end exclusive allows you to do things like specify (count collection) as the endpoint without getting an NPE. That's about the biggest difference between the two approaches.
It might be that the indexing was chosen in order to be consistent with Java libraries. java.lang.String.substring and java.util.List.subList both have exclusive-end indexes.

Resources