Prolog, produce lists with m first members reversed [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 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

Related

Why can't we replace the def of "many" with the code using <~> in Opal? [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 1 year ago.
Improve this question
I have an question about https://github.com/pyrocat101/opal/blob/master/opal.ml.
At line 105, I replaced the definition of many with let rec many x = option [] (x <~> many x);;. Then, I got the following error message:
Stack overflow during evaluation (looping recursion?).
However, these two codes seem to be same. Why?
I used OCaml version 4.12.0.
OCaml is an eager language. Thus in
let rec many x = option [] (x <~> many x)
the inner expression many x is fully evaluated as soon as the argument x is provided. Consequently, evaluating many x requires to evaluate many x and the function loops.
The smallest fix is to avoid hiding the input argument of many:
let rec many x input = option [] (x <~> many x) input
(In other words, point-free style is not consequence free in an eager and mutable language.)
Since the inner many x is a closure here, its execution will be suspended breaking the loop.
Similarly, the inlined version
let rec many x = option [] (x >>= fun r -> many x >>= fun rs -> return (r :: rs))
achieve the same result by moving the inner expression many x in an anonymous function fun r -> ....

Algorithm that finds unique numbers that equal N(or an inputed number) [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 6 years ago.
Improve this question
language agnostic question but I wanted to know if anyone can provide guidance logically on how to create a set that through adding or subtracting could generate any number between 1 and N.
An idea.
If we used only summation, then the set consisting of powers of 2 less or equal than N would be such a minimal set. With subtraction, the powers of 3 seems to be an idea good enough for start. I suspect it may be minimal but don't have a proof.
My reasoning is the following. Suppose we have k numbers in the set. Then there are at most 3^k possible results we can get with summation and subtraction. (Note that those results contain negative integers. In fact, such a set of results is symmetric with respect to 0. And, usually, we'll get fewer than 3^k different results - some of them will simply repeat).
An example of "optimal" choice:
Let's take the first number equal to 1. Then the possible results are: -1, 0, 1. Next, take 3 - then we'll get all integers between -4 and 4. (Choice of number 2, instead of 3, is clearly inefficient.) Then, to obtain the next contiguous (and non-overlapping) sequence of integers we should take 9. And so on, until number N is reached.
We could use other numbers instead of powers of 3. In such a case, we should take care of gaps that will results in consecutive steps. And I doubt it would produce a set with fewer elements. (I may be wrong, though.)
Sort all numbers.
Remove duplicates that are next to each other (is current equal to previous?)
Add / Subtract the remains.

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