I'm having trouble with my scheme program and I was wondering if anyone knew any boards that dealt with the Scheme programming language so that perhaps I could get some help there, anyone know of any?
I'm having trouble with my scheme program and I was wondering if anyone knew any boards that dealt with the Scheme programming language so that perhaps I could get some help there, anyone know of any?
maybe this
http://www.codecomments.com/Scheme/
What do you need help with? Fire away the questions.
;readTillNum void -> num
(define (readTillNum)
(local [(define answer (read))]
(if (number? answer)
answer
(readTillNum))))
;usersNumbers void -> void
(define (usersNumbers)
(display "Type in a number")
(define num1 (readTillNum))
(display "Type in another number")
(define num2 (readTillNum))
(display "Type in another number")
(define num3 (readTillNum))
(define sum (sum num1 num2 num3))
(define avg (average num1 num2 num3))
(define numPos (count-positive num1 num2 num3))
(display "the sum is")
(display sum)
(newline)
(display "the average is")
(display average)
(newline)
(display "the number of positive numbers is")
(display numPos))
im trying to write a program that reads in 3 numbers from the user, and prints the sum, average, and number of positive numbers...but...when i try to run it i get a
define: expected only one expression for the function body, but found at least one extra part in: (define num1 (readTillNum))
error. I'm new to scheme...and i can't understand the reason for the error
The problem is in scheme, functions can only consist of one expression. In your case your function contains multiple expressions.
There's one way around this, that I know of. There may be a better way but this is the method I know:
For example:
The begin statement executes every statement in the block, allowing you to have infinite number of statements. Note that all but the last statement's return values are disregarded, so the function will return the value of the last statement executed.Code:(define (func) (begin ....))
E.g:
Will output 3.Code:(define (func) (begin (+ 3 5) (+ 1 2)))
Also, you can't define variables in a begin statement, so you'll need a local around that if you want to define any variables:
Also, I noticed there's a couple areas of your code that use wrong variable names (average where I think you meant avg).Code:(define (func) (local [(define myvar 0)] (begin (set! myvar (readTillNum)) ....)))
;;sum num num num -> num
;(define (sum num1 num2 num3)
; (+ num1 num2 num3))
;
;"Examples of sum"
;(sum 0 0 0) "should be 0"
;(sum 0 0 1) "should be 1"
;(sum 0 1 1) "should be 2"
;(sum 1 2 3) "should be 6"
;(sum -1 0 1) "should be 0"
;
;;average num num num -> num
;(define (average num1 num2 num3)
; (/ (+ num1 num2 num3) 3))
;
;"Examples of average"
;(average 0 0 0) "should be 0"
;(average 0 0 3) "should be 1"
;(average 0 3 3) "should be 2"
;(average 3 3 3) "should be 3"
;(average -1 0 1) "should be 0"
;
;;count-positive num num num -> num
;(define posNum 0)
;(define (count-positive num1 num2 num3)
; (cond [(and (and (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 3)]
; [(or (or (and (positive? num1) (positive? num2)) (and (positive? num1) (positive? num3))) (and (positive? num2) (positive? num3))) (+ posNum 2)]
; [(or (or (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 1)]
; [(and (and (= num1 0) (= num2 0)) (= num3 0)) (+ posNum 0)]
; [(or (or (negative? num1) (negative? num2)) (negative? num3)) (+ posNum 0)]))
;
;"Examples of count-positive"
;(count-positive 0 0 0) "should be 0"
;(count-positive 0 0 3) "should be 1"
;(count-positive 3 3 0) "should be 2"
;(count-positive -3 -3 -3) "should be 0"
;(count-positive -1 0 1) "should be 1"
;(count-positive 14 4 2) "should be 3"
;(count-positive 0 0 -2) "should be 0"
;
;read display newline
;readTillNum void -> num
(define (readTillNum)
(local [(define answer (read))]
(if (number? answer)
answer
(readTillNum))))
;usersNumbers void -> void
(define (usersNumbers)
(local [(define sum (sum num1 num2 num3)) (define average (average num1 num2 num3))(define numPos (count-positive num1 num2 num3))]
(begin
(display "Type in a number")
(define num1 (readTillNum))
(display "Type in another number")
(define num2 (readTillNum))
(display "Type in another number")
(define num3 (readTillNum))
(display "the sum is")
(display sum)
(newline)
(display "the average is")
(display average)
(newline)
(display "the number of positive numbers is")
(display numPos))))
that's the entire code, i didn't send you the entire definitions window earlier...so i altered my code with your suggestions, however that created a new problem in that how do i pass parameters that i don't have yet...any ideas? ps. thanks for your help thus far, i appreciate it
Just define them with some default value, much like you'd do in another language.
C Version:Do the same in Scheme, by keeping the original code you had, but in the local definitions assign some default value to the variables.Code:int main() { int a=0; .... // Input for a here return 0; }
thanks... heres my code thus far
;sum num num num -> num
(define (sum num1 num2 num3)
(+ num1 num2 num3))
"Examples of sum"
(sum 0 0 0) "should be 0"
(sum 0 0 1) "should be 1"
(sum 0 1 1) "should be 2"
(sum 1 2 3) "should be 6"
(sum -1 0 1) "should be 0"
;average num num num -> num
(define (average num1 num2 num3)
(/ (+ num1 num2 num3) 3))
"Examples of average"
(average 0 0 0) "should be 0"
(average 0 0 3) "should be 1"
(average 0 3 3) "should be 2"
(average 3 3 3) "should be 3"
(average -1 0 1) "should be 0"
;count-positive num num num -> num
(define posNum 0)
(define (count-positive num1 num2 num3)
(cond [(and (and (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 3)]
[(or (or (and (positive? num1) (positive? num2)) (and (positive? num1) (positive? num3))) (and (positive? num2) (positive? num3))) (+ posNum 2)]
[(or (or (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 1)]
[(and (and (= num1 0) (= num2 0)) (= num3 0)) (+ posNum 0)]
[(or (or (negative? num1) (negative? num2)) (negative? num3)) (+ posNum 0)]))
"Examples of count-positive"
(count-positive 0 0 0) "should be 0"
(count-positive 0 0 3) "should be 1"
(count-positive 3 3 0) "should be 2"
(count-positive -3 -3 -3) "should be 0"
(count-positive -1 0 1) "should be 1"
(count-positive 14 4 2) "should be 3"
(count-positive 0 0 -2) "should be 0"
;readTillNum void -> num
(define (readTillNum)
(local [(define answer (read))]
(if (number? answer)
answer
(readTillNum))))
;usersNumbers void -> void
(define num1 0)
(define num2 0)
(define num3 0)
(define (usersNumbers)
(local [(define sum (sum num1 num2 num3)) (define average (average num1 num2 num3))(define numPos (count-positive num1 num2 num3))]
(begin
(display "Type in a number")
(= num1 (readTillNum))
(display "Type in another number")
(= num2 (readTillNum))
(display "Type in another number")
(= num3 (readTillNum))
(display "the sum is")
(display sum)
(newline)
(display "the average is")
(display average)
(newline)
(display "the number of positive numbers is")
(display numPos))))
(usersNumbers)
ok so it runs through sum average and count-positive smoothly, matching the test cases and all...when it gets to the running of usersNumbers however...i get an error message saying
local variable used before its definition: sum
odd..cause i defined it earlier...:/
never mind i realized my error in the names...now it runs but im getting odd results from the numbers im inputting...
here's the updated code...
;sum num num num -> num
(define (sum num1 num2 num3)
(+ num1 num2 num3))
"Examples of sum"
(sum 0 0 0) "should be 0"
(sum 0 0 1) "should be 1"
(sum 0 1 1) "should be 2"
(sum 1 2 3) "should be 6"
(sum -1 0 1) "should be 0"
;average num num num -> num
(define (average num1 num2 num3)
(/ (+ num1 num2 num3) 3))
"Examples of average"
(average 0 0 0) "should be 0"
(average 0 0 3) "should be 1"
(average 0 3 3) "should be 2"
(average 3 3 3) "should be 3"
(average -1 0 1) "should be 0"
;count-positive num num num -> num
(define posNum 0)
(define (count-positive num1 num2 num3)
(cond [(and (and (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 3)]
[(or (or (and (positive? num1) (positive? num2)) (and (positive? num1) (positive? num3))) (and (positive? num2) (positive? num3))) (+ posNum 2)]
[(or (or (positive? num1) (positive? num2)) (positive? num3)) (+ posNum 1)]
[(and (and (= num1 0) (= num2 0)) (= num3 0)) (+ posNum 0)]
[(or (or (negative? num1) (negative? num2)) (negative? num3)) (+ posNum 0)]))
"Examples of count-positive"
(count-positive 0 0 0) "should be 0"
(count-positive 0 0 3) "should be 1"
(count-positive 3 3 0) "should be 2"
(count-positive -3 -3 -3) "should be 0"
(count-positive -1 0 1) "should be 1"
(count-positive 14 4 2) "should be 3"
(count-positive 0 0 -2) "should be 0"
;readTillNum void -> num
(define (readTillNum)
(local [(define answer (read))]
(if (number? answer)
answer
(readTillNum))))
;usersNumbers void -> void
(define num1 0)
(define num2 0)
(define num3 0)
(define (usersNumbers)
(local [(define newSum (sum num1 num2 num3)) (define newAverage (average num1 num2 num3))(define numPos (count-positive num1 num2 num3))]
(begin
(display "Type in a number")
(= num1 (readTillNum))
(display "Type in another number")
(= num2 (readTillNum))
(display "Type in another number")
(= num3 (readTillNum))
(display "the sum is")
(display newSum)
(newline)
(display "the average is")
(display newAverage)
(newline)
(display "the number of positive numbers is")
(display numPos))))
(usersNumbers)
and no matter what i type in as the numbers...i get 0 as the answers, i.e. sum is 0 avg is 0 pos num is 0
The definition of the variables in the local (newSum..etc) are executed before the input, that is why they are all 0's right away. You need to set the values of these variables after they have been input.
Please note, this help is borderline walkthrough, so I would like you to make an attempt to fix the rest of the errors yourself. Think through your program logically, comment out code that doesn't work and comment it in slowly until your program breaks. Then figure out why it broke and fix it, one step at a time.
well yea i realized this ....and i have been working through it, hense the multiple posting...i mean you really only helped me with one thing, which i wouldnt constitute as a "walkthrough" but, if you don't want to help me anymore that's fine, i thank you for your help thusfar
anyways i figured it out myself, thanks
Many of these problems can easily be solved using some simple debugging practices. As I said, stepping through your code one line at a time is the best way to do so. The only way to get better at debugging your own code is to actually do it.
>>anyways i figured it out myself, thanks
Awesome!
Why would any sane language insist of wrapping every statement in brackets?