if: Bad syntax error (Scheme programming) - lisp

(define generalized-triangular
(lambda (input n)
(if (= n 1)
1
(+ (input n) (generalized-triangular (- n 1))))))
This program is designed to take a number and a function as inputs and do the following..
f(1) + f(2) + f(3)+ … + f(N).
An example input would be:
(generalized-triangular square 3)
The Error message:
if: bad syntax;
has 4 parts after keyword in: (if (= n 1) 1 (+ (input n) (generalized-triangular (- n 1))) input)

The error is quite explicit - an if form can only have two parts after the condition - the consequent (if the condition is true) and the alternative (if the condition is false). Perhaps you meant this?
(if (= n 1)
1
(+ (input n) (generalized-triangular input (- n 1))))
I moved the input from the original code, it was in the wrong place, as the call to generalized-triangular expects two arguments, in the right order.
For the record: if you need to execute more than one expression in either the consequent or the alternative (which is not the case for your question, but it's useful to know about it), then you must pack them in a begin, for example:
(if <condition> ; condition
(begin ; consequent
<expression1>
<expression2>)
(begin ; alternative
<expression3>
<expression4>))
Alternatively, you could use a cond, which has an implicit begin:
(cond (<condition> ; condition
<expression1> ; consequent
<expression2>)
(else ; alternative
<expression3>
<expression4>))

Literal answer
The code you posted in your question is fine:
(define generalized-triangular
(lambda (input n)
(if (= n 1)
1
(+ (input n) (generalized-triangular (- n 1))))))
The error message in your question would be for something like this code:
(define generalized-triangular
(lambda (input n)
(if (= n 1)
1
(+ (input n) (generalized-triangular (- n 1)))
input)))
The problem is input. if is of the form (if <cond> <then> <else>). Not counting if itself, it has 3 parts. The code above supplies 4.
Real answer
Two tips:
Use DrRacket to write your code, and let it help you with the indenting. I couldn't make any sense of your original code. (Even after someone edited it for you, the indentation was a bit wonky making it still difficult to parse mentally.)
I don't know about your class, but for "real" Racket code I'd recommend using cond instead of if. Racket has an informal style guide that recommends this, too.

here's the tail-recursive
(define (generalized-triangular f n-max)
(let loop ((n 1) (sum 0))
(if (> n n-max)
0
(loop (+ n 1) (+ sum (f n))))))

Since you're using the racket tag, I assume the implementation of generalized-triangular is not required to use only standard Scheme. In that case, a very concise and efficient version (that doesn't use if at all) can be written with the racket language:
(define (generalized-triangular f n)
(for/sum ([i n]) (f (+ i 1))))
There are two things necessary to understand beyond standard Scheme to understand this definition that you can easily look up in the Racket Reference: how for/sum works and how a non-negative integer behaves when used as a sequence.

Related

I can't solve Tower of Hanoi on Lisp

I know the algorithm :
(defun Hanoi (n) (if (= n 1) 1 (+ (* 2 Hanoi(- n 1)) 1)))
However, CLISP said
*** - IF: variable HANOI has no value
How could I tweak this? I can't find out the problem.
Here is your code:
(defun Hanoi (n)
(if (= n 1)
1
(+ (* 2 Hanoi (- n 1))
1)))
The error says:
*** - IF: variable HANOI has no value
So, inside the IF you are making a reference to HANOI as a variable, and there is no such variable currently bound at this point. If we look closely, we can see the following expression:
(* 2 Hanoi (- n 1))
This expressions is the application of function * to 3 arguments, namely 2, Hanoi and (- n 1). The second argument, Hanoi, stands for a variable. But there is no such variable defined here (for example, with a let). You are supposed to call the funtion #'Hanoi using the same syntax as you did for multiplication or addition, which is: wrap the function's name in parentheses along with its arguments:
(hanoi (- n 1))
See https://common-lisp.net/documentation for resources about Common Lisp.
You don't call functions as f(x) in Lisp, you call them as (f x) So your recursive call to hanoi needs to be (hanoi (- n 1)).

Sum numbers that are positive which is less than n

(defun sum (n)
(if (n<0) 0 n-1) ;; if n<0, add 0. Else add the next smallest.
(sum (n-1)))
So far I come out with something like this but I am not sure how do I declare a variable to store the sum that I would like to return.
Note that you are implementing 1+2+...+m for m = n-1, which admits a simple formula:
(lambda (n)
;; You could inject n-1 on the formula to get n.(n-1)/2
;; (like in Vatine's answer), but here I just decrement
;; the input to show how to modify the local variable
;; and reuse the formula linked above to sum up-to m.
(decf n)
(if (minusp n)
0
(/ (* n (1+ n)) 2)))
An iterative version would work too, there is no need go recursive when doing simple loops:
(lambda (n) (loop :for x :below n :sum x))
Regarding your code:
Space matters1: n<0 is read as a symbol of name "N<0" (upcased by default). The same goes for n-1 which is a symbol named "N-1".
(n<0) will attempt to run the function named n<0. The same goes for (n-1).
Comparison: you can use (minusp n) or (< n 0).
Decrement: you can use (1- n) or (- n 1).
If what you wrote was correctly written, like this:
(defun sum (n)
(if (< n 0) 0 (- n 1))
(sum (- n 1)))
... there would still be issues:
You expect your (n-1) to actually decrement n but here the if only compute a value without doing side-effects.
You unconditionally call (sum (n-1)), which means: infinite recursion. The value returned by the preceding if is always ignored.
1: For details, look at constituent and terminating characters: 2.1.4 Character Syntax Types
Edit: zerop > minusp to check for negative numbers, fixed to fit OPs question
Was some time ago I used Lisp but if I recall right the last evaluation gets returned. A recursive solution to your problem would look like this:
(defun sum (n)
(if (<= n 0) 0 ;;if n is less or equal than 0 return 0
(+ (- n 1) (sum (- n 1))))) ;; else add (n-1) to sum of (n-1)
In Lisp, all comparator functions are just that, functions, so it needs to be (< n 0) and (- n 1) (or, more succinct, (1- n)).
You don't need to keep an intermediate value, you can simply add things up as you go. However, this is complicated by the fact that you are summing to "less than n", not "to n", so you need to use a helper function, if you want to do this recursively.
Even better, if you peruse the standard (easily available on-line, as the Common Lisp HyperSpec, you will sooner or later come across the chapter on iteration, where the loop facility does everything you want.
So if I needed to do this, I would do one of:
(defun my-sum (n)
(/ (* n (1- n)) 2))
or
(defun my-sum (n)
(loop for i below n
sum i))
If I absolutely needed to make it recursive, I would use something like:
(defun my-sum (n)
(labels ((sum-inner (i)
(if (< i 1)
0
(+ i (sum-inner (1- i))))))
(sum-inner (1- n))))
This is (almost) identical to defining a global function called sum-inner, which may be preferable for debugging purposes. However, since it is very unlikely that sum-inner would have any other use, I made it local.

How to find sum of n number using clojure for loop macro?

I'm using a for-loop macro in clojure:
(defmacro for-loop [[sym initial check-exp post-exp] & steps]
`(loop [~sym ~initial]
(if ~check-exp
(do
~#steps
(recur ~post-exp)))))
I want to write a simple function to find the sum of n numbers like:
for(int i=1; i<n; i++)
sum=sum+i;
How can I do this in clojure using the for-loop macro?
First of all, there is no need to use a macro here. Second, your use of do with the final call to recur will prevent the macro from ever returning a value other than nil as discussed in Lee's answer. That you should use when instead of if when you're not really providing an else case is only a slight matter, but hints at a possible better solution: Use the else case to return the result. For this to work you need to introduce an accumulator into the equation.
(defmacro for-loop [[sym initial result initial-result check-exp post-exp] & steps]
`(loop [~sym ~initial
~result ~initial-result]
(if ~check-exp
(do
~#steps
(recur ~#post-exp))
~result)))
This is still pretty ugly, but allows you to solve your summing task like this, actually not requiring any intermediate steps:
(for-loop [i 0 r 0 (< i n) ((inc i) (+ r i))] nil)
If you macro-expand this, it's easier to see that is going on:
(pprint (macroexpand-1 '(for-loop [i 0 r 0 (< i n) ((inc i) (+ r i))] nil)))
==> (clojure.core/loop [i 0 r 0]
(if (< i n)
(do nil
(recur (inc i) (+ r i)))
r))
It would be nice if one would not have to explicitly name and use the accumulator but just use the results from the steps, but as the steps will likely need to refer to any intermediate values build up so far as in this example, there is no way around it.
(def sum (atom 0))
(def n 100)
(for-loop [i 0 (< i n) (inc i)] (swap! sum #(+ % i)))

Procedure works as intended but error message still shows up

I've been attempting to learn programming with the book "Structures and Interpretation of Computer Programs. To do the exercises I've been using DrRacket (I couldn't find a scheme interpreter for Windows 7, and DrRacket seems pretty good), and haven't had any problems so far. But while doing exercise 1.22 I've ran into an issue. I've wrote a procedure that gives a given number (n) of prime numbers larger than a:
(define (search-for-primes a n)
(define (sfp-iter a n counter)
(cond ((and (prime? a) (= counter n))
((newline) (display "end")))
((prime? a)
((newline)
(display a)
(sfp-iter (+ a 1) n (+ counter 1))))
(else (sfp-iter (+ a 1) n counter))))
(sfp-iter a n 0))
The procedure works as intended, displaying all that it should, but after displaying end it shows the following error message:
application: not a procedure;
expected a procedure that can be applied to arguments
given: #
arguments...:
#
And highlights the following line of code:
((newline) (display "end"))
What is the problem?
(I apologize for any mistakes in spelling and so, English isn't my native language, I also apologize for any error in formatting or tagging, I'm new here)
You have a couple of parenthesis problems, this fixes it:
(define (search-for-primes a n)
(define (sfp-iter a n counter)
(cond ((and (prime? a) (= counter n))
(newline) (display "end"))
((prime? a)
(newline)
(display a)
(sfp-iter (+ a 1) n (+ counter 1)))
(else (sfp-iter (+ a 1) n counter))))
(sfp-iter a n 0))
In the first and second conditions of the cond, you were incorrectly surrounding the code with (). That's unnecessary, in a cond clause all the expressions that come after the condition are implicitly surrounded by a (begin ...) form, so there's no need to group them together.

Scheme/Racket: do loop order of evaluation

The following procedure is valid in both scheme r6rs and Racket:
;; create a list of all the numbers from 1 to n
(define (make-nums n)
(do [(x n (- x 1)) (lst (list) (cons x lst))]
((= x 0)
lst)))
I've tested it for both r6rs and Racket and it does work properly, but I only know that for sure for DrRacket.
My question is if it is guaranteed that the step expressions ((- x 1) and (cons x lst) in this case) will be evaluated in order. If it's not guaranteed, then my procedure isn't very stable.
I didn't see anything specifying this in the standards for either language, but I'm asking here because when I tested it was evaulated in order.
They're generally not guaranteed to be evaluated in order, but the result will still be the same. This is because there are no side-effects here -- the loop doesn't change x or lst, it just rebinds them to new values, so the order in which the two step expressions are evaluated is irrelevant.
To see this, start with a cleaner-looking version of your code:
(define (make-nums n)
(do ([x n (- x 1)] [lst null (cons x lst)])
[(zero? x) lst]))
translate to a named-let:
(define (make-nums n)
(let loop ([x n] [lst null])
(if (zero? x)
lst
(loop (- x 1) (cons x lst)))))
and further translate that to a helper function (which is what a named-let really is):
(define (make-nums n)
(define (loop x lst)
(if (zero? x)
lst
(loop (- x 1) (cons x lst))))
(loop n null))
It should be clear now that the order of evaluating the two expressions in the recursive loop call doesn't make it do anything different.
Finally, note that in Racket evaluation is guaranteed to be left-to-right. This matters when there are side-effects -- Racket prefers a predictable behavior, whereas others object to it, claiming that this leads people to code that implicitly relies on this. A common small example that shows the difference is:
(list (read-line) (read-line))
which in Racket is guaranteed to return a list of the first line read, and then the second. Other implementations might return the two lines in a different order.