Rebol2: Determine function arity - rebol2

I suspect that this might be a colossally dumb question, but I have searched through the documentation for Rebol 2 (what there is) and have not found a good explanation. The question is this: is there a simple way to determine a function's arity in Rebol? If not, why not? It seems to me that that knowledge is crucial to being able to read Rebol code, and that it should be (more) easily available.
Of course, I can write such a function myself:
>> source arity
arity: func [f [any-function!] /local ac][
ac: 0
parse first :f [
some [[word! | lit-word! | get-word!] (ac: ac + 1)]
any [refinement! to end]
]
ac
]
But I would hardly call this simple. Can anyone explain why this isn't provided?

Related

Taking Derivative of a Matrix with functions in Maple. Want to leave functions as prime (f' or f'') and not evaluate

I want to evaluate a matrix that has a function named alpha. When I take the derivative of alpha, I would like the result to give me an alpha'
Example: if I have sin(alpha) I want to get cos(alpha)alpha' but throughout the matrix.
It is quite unclear what you mean by stating that you have a "function" in Maple, of which you intend to take the derivative.
That could mean some expression depending upon a name such as t, with respect to which you intend on differentiating using Maple's diff command. And such an expression may be assigned to alpha, or it may contain the function call alpha(t).
Or perhaps you wish to treat alpha as an operator name, and differentiate functionally with Maple's D command.
I shall make a guess as to what you meant.
restart;
Typesetting:-Suppress(alpha(t));
Typesetting:-Settings(prime=t):
Typesetting:-Settings(typesetprime=true):
M := Matrix([[ sin(alpha(t)), exp(p*alpha(t)) ]]);
map(diff, M, t);
If that's not the kind of thing that you're after then you should explain your purpose and needs in greater detail.
Your question's title mentions a desire to have something not "evaluate". What did you mean by that? Have you perhaps assigned something to the name f?
Thank you for answering. I found the solution after a lot of guess and checking and reading. Here is my code with my solution.
with(Typesetting) :
Settings(typesetdot = true);
a:= alpha(t)
Rna:= [ cos(alpha), -sin(alpha), 0; sin(alpha), cos(alpha), 0; 0, 0, 1 ]
b := beta(t)
Rab:= [ cos(beta), -sin(beta), 0; sin(beta), cos(beta), 0; 0, 0, 1 ]
Rnab:= Rna . Rab
Rnab:= map(diff, Rnab, t)
Sorry for the multiple answers, I am getting use to the website.

Filter an collection of tuples

I'm playing with iterables and comprehension in Julia and tried to code simple problem: find all pairs of numbers less then 10 whose product is less then 10. This was my first try:
solution = filter((a,b)->a*b<10, product(1:10, 1:10))
collect(solution)
but I got error "wrong number of arguments". This is kind of expected because anonymous function inside filter expects two arguments but it gets one tuple.
I know I can do
solution = filter(p->p[1]*p[2]<10, product(1:10, 1:10))
but it doesn't look nice as the one above. Is there a way I can tell that (a,b) is argument of type tuple and use something similar to syntax in first example?
I don't think there's a way to do exactly as you'd like, but here are some alternatives you could consider for the anonymous function:
x->let (a,b)=x; a*b<10 end
x->((a,b)=x; a*b<10)
These can of course be made into macros if you like:
macro tup(ex)
#assert ex.head == :(->)
#assert ex.args[1].head == :tuple
arg = gensym()
quote
$arg -> ( $(ex.args[1]) = $arg; $(ex.args[2]) )
end
end
Then #tup (a, b) -> a * b < 10 will do as you like.
Metaprogramming in Julia is pretty useful and common for situations where you are doing something over and over and would like specialized syntax for it. But I would avoid this kind of metaprogramming if this were a one-off thing, because adding new syntax means learning new syntax and makes code harder to read.

A list of variables used by Wolfram Mathematica function

Is there a way to get a list of variables used by a function?
For example:
a=1;
b=2;
f[x_]:= 2a*x+b;
Needed:
SomeFunction[f]
Output:
{{x},{a,b}}
The parameters of the function ({x}) are not really mandatory.
Thanks.
To get all the symbols (not necessarily variables) you could start with something like this:
DownValues[f]
which yields:
{HoldPattern[f[x_]] :> 2 a x + b}
The problem is then processing this in a way that you don't let Mathematica do the substitution. This is done with Hold:
held=(Hold //# DownValues[f][[1]])[[1, 2]]
which yields:
Hold[Hold[Hold[2] Hold[a] Hold[x]] + Hold[b]]
You can extract all the stuff that looks like a symbol with:
Cases[held, Hold[_Symbol], Infinity]
and you get:
{Hold[a], Hold[x], Hold[b]}
To make this a little nicer:
Union[Flatten[Hold ## Cases[held, Hold[_Symbol], Infinity]]]
which gives you:
Hold[a, b, x]
You still need the Hold, because as soon as you lose it Mathematica will evaluate a and b and you'll lose them as symbols.
If you notice, it's considering x a symbol, which you may not want because it's a parameter. You can tease the parameters out of the left side of the DownValues[f][[1]] RuleDelayed (:>) expression, and extract them, but I'll leave this detail to you.

Why don't I need to check in the member_of_set if Element and Element1 are different?

When using sets, adding an Element to the Set is done like this:
add_to_set(Element, [], [Element]).
add_to_set(Element, [Element | Set], [Element | Set]).
add_to_set(Element, [Element1 | Set], [Element1 | NewSet]) :-
not(Element = Element1),
add_to_set(Element, Set, NewSet).
Now, with this, I thought member_of_set would be like:
member_of_set(Element, [Element|_]).
member_of_set(Element, [Element1|Set]) :-
not(Element = Element1), /* Not necessary */
member_of_set(Element, Set).
This works like a charm, but in this case, not(Element = Element1) is not necessary. I can't seem to figure out why. If you ask for more answers from Prolog, won't it backtrack and succeed on the second clause of member_of_set?
An if it's not necessary in the member_of_set, then why is it necessary in the add_to_set?
Please keep in mind that I'm only studying since one month Prolog, so I'm still in some kind of mind switch...
I know that using cut, there's probably better alternatives, but cut shouldn't be used.
it's not necessary in the member_of_set
because it doesn't hurt if the list is not a set. Only you will end up - in case you pass a list with repeated elements inside - with multiple solution, but still each solution is valid.
OTOH, add_to_set leads to invalid data if you remove the test:
?- add_to_set(1,[],A),add_to_set(1,A,B).
A = B, B = [1] ;
A = [1],
B = [1, 1] ;
false.
for a friendly explanation of cuts, and why are necessary in Prolog, see this page
contains(Element, [Element|_]).
contains(Element, [_|Set]) :-
contains(Element, Set).
You can read this as: "Element is in a Set, if it's the head of that Set, or if it's in the tail of that Set". If you're checking if it's in the tail, there's no need to check if it's equal to the head, except for performance reasons.
Actually, this way, you're not even restricting it to a Set. It's a general contains.

the lambda(accounting) or factorial in prolog with tree

i have this test and my teacher is going to use one of these subjects in prolog and its tree.
but i have no idea how to implement them with prolog(since i dont even know the language).
i found something like this
This is just an example:
The following diagram shows a complete Prolog derivation tree for the goal ?-p(X).
as i said this is just an example of prolog's tree,what i need is how lambda or factorial is in prolog and its tree.
p.s: this is one of the homework's of terrence w pratt(programming languages design and implementation)
in prolog we have clauses,rules,goals and solution,for factorial how i can get these?
http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlch3 - this should help with factorial.
remember factorial is recursion so in java, it would be something like this:
public int factorial (int x){
if (x != 0){
return x* factorial(x-1);
}
else {
return 1;
}
}
I think you are looking in the wrong place for code for "factorial" or "lambda" in Prolog. Google is the place to look. It took me less than 5 minutes to find code on Google for factorial and lambda.
I tried to post the code here but it said "the code was not properly formatted - please indent all code by 4 spaces", so I gave up.

Resources