LISP Program for making a SImple Summation formula Σi. My code is Returning NIL and then the value I want, Can i not get NIL somehow? - lisp

Question Asked
(defun a-sum(n p)
(setq sum 0)
( loop for i from n to p
do(setq sum (+ sum i))
)
(format t "~d" sum)
)
My code works for all of my Test Cases but using the loop method I keep returning a NIL right before my value. Is there any way to stop this? Or maybe an alernative method I'm meant to use when faced with a probem like this?

Here is your code formatted in a readable way, please refer to https://stackoverflow.com/help/formatting when asking question and follow the conventional ways of formatting the programming language your question is about:
(defun a-sum (n p)
(setq sum 0)
(loop for i from n to p
do (setq sum (+ sum i)))
(format t "~d" sum))
There are some problems here, notably:
you call SETQ on symbol sum, but there is no variable in scope that is declared with such a name. You introduce variables with let, for example:
(let ((sum 0))
;; here you are allowed to use SETQ
(setq sum 1))
Strictly speaking, your code is not a conforming program for Lisp, but it still works: the call to SETQ does modify the symbol-value of sum, so that's as-if you used a global variable. This is usually not a good idea since then your functions have effects that are not localized to their body, but also change the environment.
In a function body, the last expression is the value returned by the function, so here the value being returned is the result of evaluating (format ...). In the case you format to a stream, which is the case here, the return value is always NIL. That's why you have a NIL result. If you want to return sum then you need to have sum as the last expression in your function.
Generally speaking, a function should do one thing, not mix different actions together: either you compute a sum, or you print it, but try not doing both at the same time (except when debugging).
The loop construct is powerful enough to do the job with needing to use an intermediate sum, calling do (setq ...), etc. Read LOOP for black belts and you should be able to rewrite it more concisely.
The sum of consecutive numbers is a well-known formula that admits a solution without loops.

I have been resisting giving an answer, because instructors should not ask students questions like this (see below).
The question is to write a function which computes the sum of i from n to p, where n and p are integers, n and p >= 0 and p >= n (the question does not state this latter requirement, and it's easy to relax it in the answer, but let's assume it).
Well, before you write some laborious and futile loop, think a bit. Write out the sum by hand:
s = n + n+1 + ... + p
= (n + n+1 + ... + p
+ p + p-1 + ... + n)/2
= (n+p + n+p + ... + n+p)/2
And now there are (p - n + 1) terms in this sum, all of which are n+p. So
s = (p - n + 1)*(n+p)/2
Or
(defun a-sum (n p)
(/ (* (+ (- p n) 1)
(+ n p))
2))
And here's why you do this:
> (time (a-sum/mindless 0 1000000000))
Evaluation took:
6.716 seconds of real time
6.716005 seconds of total run time (6.713082 user, 0.002923 system)
100.00% CPU
0 bytes consed
500000000500000000
> (time (a-sum 0 1000000000))
Evaluation took:
0.000 seconds of real time
0.000003 seconds of total run time (0.000002 user, 0.000001 system)
100.00% CPU
0 bytes consed
500000000500000000
So here's the thing: if you are the instructor and you're reading this (which I am sure you are, because I would be) don't ask questions which have well-known closed-form solutions and expect students to write the terrible brute-force solution, because doing that is teaching people to be bad programmers, and you should not do that.

Related

Processing raw binary data with Racket

I am new to Racket Lang and previously I wrote thousands of lines of code in C++, Java and C. I am trying to figure out how to do the following task:
Given an array (like C uint8_t array) with the following format:
First byte is used to indicate the "format", let's say this could be 0x0a, 0x0b and so on.
Remaining data may include C strings without the null terminator and integers.
Write a function that parses the array and puts the values in some variables.
Before asking here, I was reading: https://docs.racket-lang.org/guide and also https://docs.racket-lang.org/reference
My approach is as follows:
I am using a byte string because it seems it can be used to mimic the C++/C arrays.
I am using tail recursion to traverse the "array".
Questions:
1) In C, I always use the return value of a function as an error code: 0 is ok and any negative value is an error. With Racket, I am using multiple return values to indicate: a) the return code, b) the processed value(s), i.e. something like:
(values return_code out1 out2 ...)
What do you think? Do you recommend the use of exceptions for error handling?
2) What's the best approach to process arrays in Racket? I mean, the best according to the offered by Racket and to achieve a good performance.
Thanks!
Edit1:
Regarding my first question (the return codes), I am calling many functions and I would like to return an exit code that helps me to know if there was an error inside the function. This is a sample code:
#lang racket
(define (is_valid in)
(cond
[(and (>= in 10) (<= in 15)) #t]
[else #f]))
(define (copy_values in)
(define len (bytes-ref in 2))
; 3 is where the string "ABCD" begins
(define str (subbytes in 3 (+ 3 len)))
(define numbers (subbytes in (+ 3 len)))
(values str numbers))
(define (parse in)
(define type (bytes-ref in 0))
(if (is_valid type)
(let ()
(define-values (str numbers) (copy_values in))
(values #t str numbers))
(values #f 0 0)))
; format: type strlen
; len |-- str --| | -- numbers -- |
;
(define input1 (bytes 10 10 4 65 66 67 68 110 120 130 140 150))
(define input2 (bytes 1 10 4 65 66 67 68 110 120 130 140 150))
(parse input1)
(parse input2)
This is the output in DrRacket:
Welcome to DrRacket, version 6.7 [3m].
Language: racket, with debugging; memory limit: 128 MB.
#t
#"ABCD"
#"nx\202\214\226"
#f
0
0
Look how I use the (values ...) stuff, does that make sense?
To return a value to the operating system, use (exit [v]).. Other values should probably go to standard out and/or standard error. This means writing them before the program exits.
In terms of assigning the values of an array to variables, there are many ways to do it depending on how lexical scope comes into play within the function and module and across modules. Also reference semantics versus value semantics are considerations.
Finally, there is no compelling reason to choose recursion over an explicit loop when dealing with a byte-string. bytes-length is a low level primitive implemented in C and will return the length needed for a loop without testing for an empty byte-string.

Explain me my LISP code

I'm new in LISP and I'm trying to understand recursion.
What I know is that recursion needs a STOP condition. In my code below, can you explain to me why (equal x 0) 1 is my STOP condition SINCE fact(- X 1) could continue indefinitely as in my second condition, I have set to t the second line of my cond which means it should continue.
BUT when I run the program, it works fine though. Below is my code (found by chance)
(defun fact(x)
(cond
((equal x 0) 1)
(t (*(fact(- x 1)) x))
)
)
cond expresssions have a number of clauses. Each clause is of the form (expr1 expr2). If the expr1 evaluates to true then expr2 is evaluated and that is the returned value of thecond`. No other clauses are evaluated.
So, once x becomes 0 the first clauses of cond evaluates to true and that call to fact returns 1.
The other clause of cond has a first expression of t which is true by definition; thus if the first clause is not used it always uses the second clause. (A cond clause with t is like an "else" in an "if" statement in other langugaes.)
This function is recursive, if you call it with an argument of 2, it checks if 2 == 0, it doesn't so then it multiplies 2 with the value returned by recursively calling itself with 1. Since 1 != 0 it will return the value of 1 multiplied by the value of recursively calling itself with 0. Since 0 does equal 0 it just returns 1, which is used one layer to return 1 which is then used at the top layer of this recursion to return 2.
Common Lisp has a function trace which will let you see when your function is called and with what arguments. Typically each recursive call will be indented so you can see something like this:
(fact 2)
(fact 1)
(fact 0)
Which can be helpful for understanding this function.
(As mentioned in a comment - this function doesn't catch the case of a negative input - but that is error handling and could be easily fixed).

Emacs calc multiple solutions

I have this minimal code:
(progn
(calc)
(calc-hyperbolic)
(calc-eval "[8.66e10 = r * v, -7.51e6 = 0.5*v^2 - 6.67e-11*6e24/r]" 'push)
(calc-solve-for "[r,v]")
(print (calc-eval 1 'top))
(calc-quit))
And I get the generic solution
"[r = 86600000000. / (4621.24711316 - 2517.12631405 s2), v = 4621.24711316 - 2517.12631405 s2]"
In the manual I read:
"The Hyperbolic flag (H a S) [fsolve] tells the solver to report the
fully general family of solutions. It will invent variables n1, n2, …,
which represent independent arbitrary integers, and s1, s2, …, which
represent independent arbitrary signs (either +1 or -1)." ... "Note
that variables like n1 and s1 are not given any special interpretation
in Calc except by the equation solver itself. As usual, you can use
the s l (calc-let) command to obtain solutions for various actual
values of these variables."
How can I proceed programmatically to get the two solutions, aka, substitute s2 (I do not know why s2 instead of s1 ???) by +1 and -1?
Thanks.
(progn
(calc)
(calc-hyperbolic)
(calc-eval "[8.66e10 = r * v, -7.51e6 = 0.5*v^2 - 6.67e-11*6e24/r]" 'push)
(calc-solve-for "[r,v]")
(calc-eval -1 'push) ; value for variable s2
(calc-let 'var-s2) ; right name of variable s2
(print (calc-eval 1 'top))
(calc-quit))
Thank you.

sqrt function gets error in racket

I'm trying to build a simple function that gets a number, checks if the number is more the zero and return the square root of the number:
#lang pl 03
(: sqrtt: Number -> Number)
(define (sqrtt root)
(cond [(null? root) error "no number ~s"]
[( < root 0) error "`sqrt' requires a non-negative input ~s"]
[else (sqrt root)]))
but the result I get when I'm trying to compile the function is:
type declaration: too many types after identifier in: (: sqrtt: Number
-> Number)
Why am I getting that error and how do I fix it?
Try this:
(define (sqrtt root)
(cond [(null? root) (error "no number ~s")]
[(< root 0) (error "`sqrt' requires a non-negative input ~s")]
[else (sqrt root)]))
You simply forgot the () around error. Remember that error is a procedure and, like all other procedures, to apply it you have to surround it with parentheses together with its arguments.
The error message you're getting tells you that you have too many types after an identifier in a : type declaration. Now in racket, sqrtt: counts as an identifier. What you probably meant was sqrtt :, with a space in between.
(: sqrtt : Number -> Number)
The difference is that type declarations of the form (: id : In ... -> Out) are treated specially, but those of the form (: id In ... -> Out) are not. And sqrtt: is counts as the id.
There's also the problem Oscar Lopez pointed out, where you're missing parens around the error calls. Whenever you call a function in racket, including error, you need to wrap the function call in parens.
Also, the (null? root) clause is useless, since root has the type Number and null? will always return false for numbers.
And another thing, depending on what the pl language does, if you get a type error from < afterwards, that's because < operates on only Real numbers, but the Number type can include complex numbers. So you might have to change the type to Real or something.

Two simple push functions; one permanently mutates global var, other doesn't, why?

Here are two simple functions that use push on a variable passed in:
(defun push-rest (var) (push 99 (rest var)))
and
(defun just-push (something) (push 5 something))
The first one will permanently mutate the var passed. The second does not. This is quite confusing for someone who is learning the scoping behavior of this language:
CL-USER> (defparameter something (list 1 2))
SOMETHING
CL-USER> something
(1 2)
CL-USER> (just-push something)
(5 1 2)
CL-USER> something
(1 2)
CL-USER> (push-rest something)
(99 2)
CL-USER> something
(1 99 2)
In push-rest why isn't the var's scope local to the function like in just-push, when they are both using the same function, push?
According to Peter Siebel's Practical Common Lisp, Chapter 6. Variables: This might help you a lot:
As with all Common Lisp variables, function parameters hold object references. Thus, you can assign a new value to a function parameter within the body of the function, and it will not affect the bindings created for another call to the same function. But if the object passed to a function is mutable and you change it in the function, the changes will be visible to the caller since both the caller and the callee will be referencing the same object.
And a footnote:
In compiler-writer terms Common Lisp functions are "pass-by-value." However, the values that are passed are references to objects.
(Pass by value also essentially means copy; but we aren't copying the object; we are copying the reference/pointer to the object.)
As I noted in another comment:
Lisp doesn't pass objects. Lisp passes copies of object references to functions. Or you could think of them as pointers. setf assigns a new pointer created by the function to something else. The previous pointer/binding is not touched. But if the function instead operates on this pointer, rather than setting it, then it operates on the original object the pointer points too. if you are a C++ guy, this might make much more sense for you.
You can't push on a variable passed. Lisp does not pass variables.
Lisp passes objects.
You need to understand evaluation.
(just-push something)
Lisp sees that just-push is a function.
Now it evaluates something. The value of something is a list (1 2).
Then it calls just-push with the single argument (1 2).
just-push will never see the variable, it does not care. All it gets are objects.
(defun push-rest (some-list) (push 99 (rest some-list)))
Above pushes 99 onto the rest, a cons, of the list passed. Since that cons is visible outside, the change is visible outside.
(defun just-push (something) (push 5 something))
Above pushes 5 to the list pointed to by something. Since something is not visible outside and no other change has made, that change is not visible outside.
push works differently when it's passed a symbol or list as it's second argument. Pehaps you might understand it better if you do macroexpand on the two different.
(macroexpand '(push 99 (rest var)))
;;==>
(let* ((#:g5374 99))
(let* ((#:temp-5373 var))
(let* ((#:g5375 (rest #:temp-5373)))
(system::%rplacd #:temp-5373 (cons #:g5374 #:g5375)))))
Now most of this is to not evaluate the arguments more than once so we can in this case rewrite it to:
(rplacd var (cons 99 (rest var)))
Now this mutates the cdr of var such that every binding to the same value or lists that has the same object in it's structure gets altered. Now lets try the other one:
(macroexpand '(push 5 something))
; ==>
(setq something (cons 5 something))
Here is creates a new list starting with 5 and alters the local functions binding something to that value, that in the beginning pointed to the original structure. If you have the original structure in a variable lst it won't get changed since it's a completely different binding than something. You can fix your problem with a macro:
(defmacro just-push (lst)
(if (symbolp lst)
`(push 5 ,lst)
(error "macro-argument-not-symbol")))
This only accepts variables as argument and mutates it to a new list having 5 as it's first element and the original list as it's tail. (just-push x) is just an abbreviation for (push 5 x).
Just to be clear. In an Algol dialect the equivalent code would be something like:
public class Node
{
private int value;
private Node next;
public Node(int value, Node next)
{
this.value = value;
this.next = next;
}
public static void pushRest(Node var)
{
Node n = new Node(99, var.next); // a new node with 99 and chained with the rest of var
var.next = n; // argument gets mutated to have new node as next
}
public static void justPush(Node var)
{
var = new Node(5, var); // overwrite var
System.out.print("var in justPush is: ");
var.print();
}
public void print()
{
System.out.print(String.valueOf(value) + " ");
if ( next == null )
System.out.println();
else
next.print();
}
public static void main (String[] args)
{
Node n = new Node( 10, new Node(20, null));
n.print(); // displays "10 20"
pushRest(n); // mutates list
n.print(); // displays "10 99 20"
justPush(n); // displays "var in justPush is :5 10 99 20"
n.print(); // displays "10 99 20"
}
}
(push item place)
It work as follows when the form is used to instruct the place where this is referred to in the setf:
(setf place (cons item place))
Basedon your profile, it looks like you have familiarity with C-like languages. push is a macro, and is the following equivalence is roughly true (except for the fact that this would cause x to be evaluated twice, whereas push won't):
(push x y) === (setf x (list* x y))
That's almost a C macro. Consider a similar incf macro (CL actually defines an incf, but that's not important now):
(incf x) === (setf x (+ 1 x))
In C, if you do something like
void bar( int *xs ) {
xs[0] = xs[0] + 1; /* INCF( xs[0] ) */
}
void foo( int x ) {
x = x + 1; /* INCF( x ) */
}
and have calls like
bar(zs); /* where zs[0] is 10 */
printf( "%d", zs[0] ); /* 11, not 10 */
foo(z); /* where z is 10 */
printf( "%d", z ); /* 10, not 11 */
The same thing is happening in the Lisp code. In your first code example, you're modifying contents of some structure. In your second code example, you're modifying the value of lexical variable. The first you'll see across function calls, because the structure is preserved across function calls. The second you won't see, because the lexical variable only has lexical scope.
Sometimes I wonder if Lisp aficionados (myself included) promote the idea that Lisp is different so much that we confuse people into thinking that nothing's the same.