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

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)))

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 -> ....

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

implicit variables in R [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 5 years ago.
Improve this question
In scala we have the concept of an implicit variable or parameter, which can be handy, although sometimes confusing, in many cases. The question is:
Is there something like implicit variables in R?
If there is not, would be possible to achieve the same behavior as scala implicit parameters while calling some function in R?
Moved from comments.
If I understand this correctly an implicit parameter to a function is a function argument which, if not specified when calling the function, defaults to a default associated with that argument's type and only one such default can exist for all instances of that type at any one time; however, arguments in R don't have types -- its all dynamic. One does not write f <- function(int x) ... but just f <- function(x) ... .
I suppose one could have a convention that integerDefault is the default value associated with the integer type:
f <- function(x = integerDefault) x
g <- function(y = integerDefault) y + 1L
integerDefault <- 0L
f()
## [1] 0
g()
## [1] 1
There is nothing that will prevent you from passing a double to f and g but
if you don't pass anything then you get the default integer which seems similar to scala and
there can only be one such default at any point since they all go by the same name which seems similar to scala. Also
if no value is assigned to integerDefault then the function fails which is also similar to scala.
Note that integerDefault will be looked up lexically -- not in the caller.
I'm not sure what the desired behavior is. From the first paragraph of the site you link, it seems to be simply a default parameter setting for parameters not provided to the function. This is used in R all the time:
> f <- function(x=10) print(x)
> f()
[1] 10
Is that what you mean?

Code to calculate a certain arithmetic expression in assemlby [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 5 years ago.
Improve this question
Expression: z=a+b * b-(36/(b * b)/(1+(25/(b * b)))
I have no idea what data directives I should use and in what order I should write the code.
C = A + B for Z80 CPU:
ld a,A
ld b,B
add a,b ; a = C
C = A * B for 68000 CPU:
MOVE.W D0,A
MOVE.W D1,B
MULS.W D1,D0 ; D0 = C
et cetera ... check your target CPU instruction guide to see what arithmetic operations it does implement directly, and what operand types can be used for them, which registers you have available, and their data type...
Looks like you don't have to write universal math expression parser (this gets tricky quite quickly, once at high-school we had on programming competition the task to write exactly that, and at first we were like "what, a single task for 5h time, I will be done in 30min" ... then after 5h nobody's code passed the full test suite, best were around 80% correct).
So if only this particular expression should be calculated, you can "parse" it by hand, simplifying it down into particular steps involving only single operation and one of intermediate sub-results. Then just write that with your instructions, step by step, like you would calculate that by hand (also make sure you conform to the math expression calculation rules, you know which operations have priority over others? Parentheses override anything, then mul/div first, add/sub later, from left to right, but this is base school math stuff, so unless you are 10y.o., you shouldn't ask this).
If your CPU does not have instruction for division or multiplication, simply implement it by subtraction/addition in loop. It's very lame performance wise, but if you have to ask this question, then one can not expect you would even comprehend more advanced algorithm for these.

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);

Resources