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 days ago.
Improve this question
(defun add-them(a b)
(loop
(if(< a 15)
(setq a (* a b))
(write a)
)
(when(> a 15)
(return a))
)
)
(print (add-them 3 5))
Having issues with this code block I want to be able to check if a is less than 15 and multiply it by b if it is less than 15 in a loop. Then I want to return it if it is greater than 15. I've never used lisp before and this is for an assignment so please do not outright give me the answer, just guidance.
Check your syntax. The if statement takes three arguments: the condition, the code when the condition is true and the code when the condition is false.
(if <test>
<do-this-when-true>
<do-this-when-false> )
Each one of these must be a list, so if you need to do multiple things surround it with e.g. progn.
In your example it will look like this
(loop
(if (< a 15)
(progn
(setq a (* a b))
(write a))
(return a))
)
As mentioned in another comment, this is not very lispy but it should be a good start.
There is really no purpose in learning Lisp if you end up just replicating code that could have been written in Fortran. It's better to think about a different way to solve the problem. In particular this way:
To answer the assignment:
start with a and b:
if a >= 15 the answer is a,
otherwise answer the assignment with a * b and b.
Related
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
So, the question was find the greatest number using "if" statement between three numbers. I wrote the program myself and I'm a newbie to programming so, I tried to run this program but it showed 2 errors repeatedly.
error 1: x is undefined
error 2: y is undefined
So, I understand that it wants me to define the value of x,y but what I want to do is to firstly Compare the value of a and b and then compare the value of c with the greater number I got by comparing a and b.
I'm pasting my code below:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter any three number::");
scanf("%d%d%d",&a,&b,&c);
if(x=(a>b)||(b>a));
printf("\n(a>b)||(b>a)=%d",&x);
{
if(y=(x>c)||(c>x));
printf("\n(y>c)||(c>y)=%d",&y);
}
getch();
return 0;
}
How can I improvise this? Hoping for positive response!
The problem is, you never defined x and y, as your compiler pointer out. Just define them like you did for other variables.
int a,b,c,x,y;
would do the job.
That said
you don't really need those if statements, as you used here.
The result of the relational operators (< / >) are not the numbers, it's an integer value, either 0 or 1.
For %d, you don't need to supply the address of the variable, just the variable is enough.
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 3 years ago.
Improve this question
I don't get what I should put in the place of: "α" and "β" in order to print all the 3 digit numbers which are different from 0 and from one another.
It is required to replace those 2 variables, with some code
We are entering G(0)!!!
It's from an exam paper, I really don't get it, please help.
void G(int k)
{int i;
for(i=1;i<=α;i++)
{ p[k]=i;
if(β)G(k+1);
else
printf("%d%d%d\n",p[0],p[1],p[2]);
}
}
For any of this to make sense, it must be that p is declared as a global int array of dimension at least 3. I assume for the purposes of this answer that it is in fact so declared.
Note that the function sets p[k] = i, but it later reads back only p[0], p[1], and p[2]. This should give you a pretty good idea about what makes sense for expression β, which controls whether to recurse (increasing k) or print.
Note also that the function sets p[k] = i, and that when it reads back those p[k] for various k, it wants to get values ranging from 1 to 9 (no more and no less). This should give you a pretty good idea of what expression makes sense for α, the inclusive upper bound on i.
Having figured those out, it remains to satisfy yourself that the natural substitutions for those expressions indeed produce a resulting function that behaves as required when initially called as G(0). I suspect that you will find that easier than you did discerning the needed expressions.
(Details are left as an exercise.)
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
In the following C code, f calls g, and g calls h. Notice the goto in h, however: it will jump back to f if a satisfies a certain condition.
void h(int a)
{
if (a > 10)
goto out;
}
void g(int a, int b)
{
h(a);
}
void f(int a, int b)
{
g(a, b);
return;
out:
printf("b: %d\n", b);
}
My question is: how will the stack be if the goto is triggered? Will g and h be unstacked? And will f still print the right value of b? (or will it print it right only in some cases when I am lucky?)
(Please, I don't want to discuss if this is a good practice, or if this should be used at all. Also, consider that the actual code is complicated enough so that the compiler won't be smart enough to, e.g., optimize g out)
[I can give details on why I am doing this, if it matters -- I don't think it does]
This will result in undefined behavior in standard C.
From 6.8.6.1/1 of the C Language Standard:
The identifier in a goto statement shall name a label located
somewhere in the enclosing function. A goto statement shall not jump
from outside the scope of an identifier having a variably modified
type to inside the scope of that identifier.
The question is void, because it simply cannot be done like this: you only can goto within a function, not between functions.
For jumping between functions, you can use setjmp/longjmp.
A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function.
Labels are local to a single function, you cannot jump between different functions.
NOTE − In my opinion the use of goto statement is highly discouraged.
ref: http://www.tutorialspoint.com/cprogramming/c_goto_statement.htm
You cannot do this since labels are local to each particular function. However, the nearest standard equivalent is the setjmp() and longjmp() pair of functions. That should work. :)
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
What's really happening behind?
int a=10,b=5,c=3;
b!=!a;
c=!!a;
Why the values of b and c are 5 and 1 respectively?
b is 5 because you've assigned it that value and never changed it, since
b!=!a;
...is just a condition that you don't do anything with, not any form of assignment.
c is 1 because a is 10 and !10 is 0, and !0 is 1, thus !!a is 1 (a is 10).
You already have got the answer to your question, but just to have a deeper look into why it happens that way, let me add my two cents here.
An expression like
b!=!a;
is same as
b != !a;
because of the maximal munch rule used in the translation phase. Basically, this says that the longest possible token should be selected from the input while creating a construct (obviously, valid/ meaningful).
Following this principle the ! and = are considered together to form the perfectly valid operator != and the expression is parsed like
b != !a;
over
b ! = !a; //or anything else.
That is why, there is no assignment, as you might have thought.
That said, ! is an unary operator, which does not change the value of the operand. So, taken together, your code is essentially same as
int a=10,b=5,c=3;
c=!!a; //double negation
so, a and b are unchanged, and c is 1 (because !!10 == !0 == 1 )
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
Today I came across a question. It says that:
You need to find if a number can be expressed as sum of two perfect powers. That is, given x find if there exists non negative integers a, b, m, n such that a^m + b^n = x where 1 <= x <= 1000000 and m > 1, n > 1
Could someone please explain me how this can be done?
I know that we can write something like this:
for(int a = 1; true; a++){
for(int b = 1; true; b++){
// And so on and so forth
}
}
But this is not the very efficient (or correct) way of doing so.
Thanks.
Sometimes, that is the only way to solve these kind of problems. The one you have posted belongs to a class of problems called "one way functions": there is a trivial implementation of the problem in one way...
Given four non negative integers, a, b, m and n, find x so a^m + b^n = x
But the other way...
Given x, find four non negative integers a, b, m and n, so a^m + b^n = x
is non trivial. In fact, it could be impossible to solve it, or your best chance, have to use a force brute algorithm to solve it, which is what you have proposed.