Number of characters in Postfix notation when number in Infix notation is known - character

I am converting expressions with only function calls and integers from Infix notation to Postfix notation (only letters,digits,commas,brackets and no spaces).
For example, add(add(1,2),add(3,4)) to 1 2 add 3 4 add add.
Input expression is 22 characters long, output is 19, by 3 shorter.
sqrt(add(5,11)) to 5 11 add sqrt.
Input expression is 15 characters long, output is 13, by 2 shorter.
Will be Postfix notation always shorter by amount of characters equal to amount of functions?

Does infix require more syntax? That is an interesting question. However, the examples you give are prefix notation, rather than infix. Short answer though: Yes, infix can require more syntax in some situations (e.g. to disambiguate operator precedence).
I believe that counting necessary tokens may be more meaningful than counting characters. Let's see what happens.
In your prefix examples, by moving the opening parens to the left of the operators and replacing the commas with spaces, we have Lisp: (+ (+ 1 2) (+ 3 4)) and (sqrt (+ 5 11)).
We can then simply reverse everything and have a postfix notation (but no reduction in token or character count): ((1 2 +) (3 4 +) +) and ((5 11 +) sqrt).
Given that the arity is fixed (+ is binary, sqrt is unary), we can unambiguously remove the parens and have Forth: 1 2 + 3 4 + + and 5 11 + sqrt. In fact, with fixed arity operators the parens were not needed in any case. They never were meaningful, necessary tokens to begin with.
But does infix require more syntax? Well, because of operator precedence, this 2 × (3 + 4) is not the same as 2 × 3 + 4. The parens are necessary to indicate precedence in infix notation. Whatever syntax you choose, you will have to have something added to disambiguate this. However in postfix (or prefix) these two expressions can be represented unambiguously with equal token count (and always fewer than in infix): 2 3 × 4 + and 2 3 4 + × or 3 4 + 2 ×.
I hope that answers what you were getting at!

Related

Lisp, calculating mixed expressions

i'am currently trying lisp and i need to solve a Problem. I want to write a function that takes a list as input and returns the calculated number. The elements that are not numbers should be added at the end of the list. Its important that the caluclated number is in front. At the end of this post you can see some function calls with the output i'am looking for.
(Function '(+ 100 1 2 3 4 5 6));-> 121
(Function '(+ 1 2 A B 3 C));-> (+ 6 C B A)
(Function '(+ A B C D 0));-> (+ D C B A)
(Function '(- 2 3 4 5 6));-> -19
(Function '(- 1 B));-> (- 1 B)
(Function '(- 6 2 A B 3 C));-> (- 1 C B A)
In any Lisp you have car to fetch the first element of a list and cdr for fetch the list without the first element and cons to add a list to the beginning of another list.
identify the operation by looking at the first element. Since - in a quoted list is a symbol you cannot apply this so you need to have a list of acceptable operations.
Loop through the rest of the list accumulating the non numbers to a list and the numbers using the operation.
cons the resulting number onto the list of symbols, then the operation making it (op num . symbols)
I'm assuming that + and - don't work as math as swithcing the order when using - or any other non associative operation changes the result.

coerce function in common lisp -- arrays and lists

I am seeing different behavior of coerce between different versions of Common Lisp - wondering which one is "right" or is the standard ambiguous on this seemingly simple question:
is
(coerce '(1 2 3) 'array)
correct lisp? It works fine in Clozure Common Lisp but not in sbcl.
And when it does not work what is the easiest way to coerce a list into an array?
Thanks
The specification says:
If the result-type is a recognizable subtype of vector, and the object is a sequence, then the result is a vector that has the same elements as object.
array is not a subtype of vector -- vectors are 1-dimensional arrays, but array includes arrays with any number of dimensions.
You can use one of these
(coerce '(1 2 3) 'vector)
(coerce '(1 2 3) '(array t (*)))
In the second version, (*) specifies a single dimension whose size is unspecified.
Your use is unspecified, so implementations are free to implement it as they please. If it returns a value, the value has to be an ARRAY of some kind.
To add to Barmar's answer (this is really a comment, but it's too long), while it's fine for CCL to do what it does, I think it's clear that something like this would be very hard to define in a standard.
Consider something like this:
(coerce '((1 2 3) (4 5 6) (7 8 9)) 'array)
What is the result of this meant to be? Should it be:
a vector each of whose elements is a three-element list?
the equivalent of (make-array '(3 3) :initial-contents '((1 2 3) (4 5 6) (7 8 9)))?
the transpose of that array?
I think either of the first two are reasonable interpretations: sometimes you will want one, sometimes the other. The third is probably not reasonable given CL is a row-major language.
So if (coerce ... 'array) were in the standard, how would you specify which of these you wanted? If you just chose one, which should it be (and how do you now reach agreement with the people on the committee who think it should be the other!)?

in a function, how to pass a value to an inline sub-function

In a function, I'm doing something like this:
{3#x} each 7,8,9
/ returns (7 7 7j;8 8 8j;9 9 9j)
The following code fails (to my understanding because N is not defined in the lambda):
foo:{
N: floor acos -1;
{N#x} each x }
foo 7,8,9
My workaround is to use a projection:
foo:{
N: floor acos -1;
{y#x}[;N] each x }
Is there a shorter or neater solution?
I think the problem arises due to the lack of what is termed 'lexical scoping' within q/kdb. This essentially means that a local variable is not visible within the body of a local function defined in the same scope. The internal function here cannot 'see' N where you have defined it.
Perhaps a more concise approach would be the following
{(floor acos -1)#'x}[7 8 9]
Otherwise your workaround works just fine as you are passing in the local variable as an argument. This can be sped up slightly by noticing that you are trying to apply the "take" operator to each of the arguments to it's right therefore you can apply the "each right" operator /:...
q)\t:1000000 N:floor acos -1;f:{[N;x] N#/:x};f[N;7 8 9]
1308
q)\t:1000000 N:floor acos -1;{y#x}[;N]each 7 8 9
1835
This can be further optimised by using the "each both" operator which will evaluate both arguments of the take operator pair-wise, extending an atom argument to match the length of the list..
This is what is happening in Rahuls example:
q)\t:1000000 foo:{#'[floor acos -1;x]};foo 7 8 9
1012
Hope this helps.
Inner function does not have access to parent function variables. You need to explicitly pass the required parent variables to the inner function.
In your example, you could avoid inner lambda function by changing your code to use '#' and each-both:
q) foo:{#'[floor acos -1;x]}
q) foo 7 8 9
7 7 7
8 8 8
9 9 9
Tip: Always try implementing your logic using KDB operators and adverbs first. They can be combined in number of ways to generate efficient and simple code.
As mentioned by Rahul and Liam, the best approach is a take and each-both approach for this simple example.
There's also cases where creating a simple projection using parentheses () can work while retaining the same legibility. In your example:
q){N:floor acos -1;(N#)each x}7 8 9
7 7 7
8 8 8
9 9 9
As for variable scoping - the non-global variables are only scoped within the braces {} of a function
To avoid having to use lambdas at all, the following solution can be adopted (based on the solution provided above by Liam):
(#[floor acos -1] #)each 7 8 9
This format can help obviate the issue of lexical scoping, since it would have access to variable outside of the brackets within the bracket itself. An equivalent statement would be:
N: floor acos -1; (#[N]#) each 7 8 9
One can further manipulate the results within the brackets like how one do in a function (since its "functionalised" with the brackets to tell the interpreters on the appropriate sequence of "actions")
N: floor acos -1; (1_ #[N]#) each 7 8 9

Modular arithmetic Basic cofusion

I am just learning number theory .When I was reading modular arithmetic I came across this statement :
29 is congruent to 15 (mod 7).
So actually this statement actually shows just
29 is congruent to 15
and we are working under mod 7..mod 7 in brackets is just to show the modulus. It is not 29 is congruent to 15%7.It is 29 is congruent to 15 and we are working under modulus 7.
Your observation is correct. The word mod is actually used in two different senses: one of them is to clarify a relation as you describe
A = B (mod C)
means, e.g., that B-A is divisible by C. Or sometimes (but equivalently in the end), it means that you should be reading A and B as being notation, e.g., for elements of the algebraic structure integers modulo C rather than as notation for integers.
The other usage is as a binary operator: B mod C means the remainder when B is divided by C.
Usually it's straightforward to tell the difference from context... assuming you are actually aware of both possible usages. Also, in the first kind of usage, mod is usually set off from the others; e.g.
A = B mod C
is the first usage as a relation, but
A = B mod C
could go either way.

What is the algorithm used by programming languages to eval ASTs? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
What is the algorithm used by programming languages to eval their ASTs?
That is, suppose we have 4 basic functions, /*+-. What is a basic algorithm that will correctly eval any AST in the form of, for example:
(+ (- (* 3 2) (+ (/ 5 2))) (* 2 4))
My doubt is actually what happens if the evaluation of a node returns something that still have to be evaluated. For example, in Scheme, the evaluation of ((lambda (a) (+ a 2)) 3) would be (+ 3 2). But this could be evaluated again into 5. So how does the language determine when to stop evaluating a form?
You're totally misunderstanding how Scheme/Lisp evaluation works. I'll use the example you gave:
(+ (- (* 3 2) (+ (/ 5 2))) (* 2 4))
To evaluate a list, we evaluate each of the elements. The first is expected to return a procedure (I'm ignoring the special case of syntax operators), the rest can return arbitrary values. We call the procedure with the rest as arguments.
At the top level, this is a list of 3 elements:
+
(- (* 3 2) (+ (/ 5 2)))
(* 2 4)
Each of these is evaluated. The first is a variable whose value is a procedure (Scheme's built-in addition function). The others, being lists, require recursion into the evaluation algorithm. I'm going to skip describing the second one, because of its complexity, and go to the third: (* 2 4).
This is a list of 3 elements: *, 2, and 4. As above, * is the multiplication function. 2 and 4 are literals, so they evaluate to themselves. So we call the multiplication function with the arguments 2 and 4, and it returns 8.
The complicated second argument goes through the same process, just with several more levels of recursion. It eventually returns 4. So we then call the multiplication function with the arguments 4 and 8, and it returns 32.
Your second example is processed similarly. At the top, you have a list of two elements:
(lambda (a) (+ a 2))
3
Each of these is evaluated. Lambda is special syntax that parses its contents and returns a procedure that evaluates its body in a context where the parameter variables are bound to arguments, so the first returns a procedure that adds 2 to its argument and returns that. 3 is a literal, so it just returns the number 3. We then call the procedure with the argument 3, it adds 2 to it and returns 5.
In the case you give, the execution will stop at 5, since it is a literal value and represents itself. This is not hard to test for. You might as well ask how a function that traverses a list in depth knows how to stop (in fact, you should, since in Scheme this is the same thing).
In Scheme, any compound expression should eventually resolve to one of the 7 primitive datatypes or the empty list, unless it becomes trapped in an infinite loop. If you want to know in advance if the expression will resolve, well, that's an interesting problem: http://en.wikipedia.org/wiki/Halting_problem
I think you may be asking the wrong question, but I will try:
Until it gets a result that it can work with. In your example you're asking about when an interpeter stops evaluating an expression... its 100% language depedent and would be a completely different answer if you were to ask about a compiler. For your Scheme example, you would need to read the Scheme specification (R5RS).
So it is defined by the writer of the interpreter. If a single literal (or even variable) is the expected result of an expression in my language, then it would stop there.
There are many different algorithms.
Alternative 1: You could compile the AST to an intermediate representation which is more linear. Your code could be compiled to something like the following:
a <- 3 * 2
b <- 5 / 2
c <- a - b
d <- 2 * 4
e <- c + d
return e
This is easy to evaluate, since it is just a sequence of instructions. Most of the instructions have the same format: X <- Y OP Z, so the evaluator will be very simple.
Alternative 2: You can compile alternative #1 to machine code or byte code.
li r3, 3
muli r3, 2
li r4, 5
divi r4, r5, 2
subf r3, r3, r4
li r4, 2
muli r4, r4, 4
add r3, r3, r4
blr
Alternative 3: You can compile alternative #1 to a special form called SSA, or "single static assignment", which is similar to #1 but the LHS of every assignment is unique, and special "phi" nodes are used to combine values from different branches. SSA can then be compiled to machine code or byte code.
Alternative 4: You can evaluate the AST by recursive descent. This is covered thoroughly in most books on Scheme / Lisp.
Alternative 5: You can use recursive descent to convert the code to stack machine code, and then evaluate that. Something like:
push 3
push 2
mul
push 5
push 2
div
sub
push 2
push 4
mul
add
ret
Alternative ∞: There are plenty of other techniques. The books written on this subject are thick.