PDA

View Full Version : Scheme

YankeePride13
12-08-2005, 03:56 PM
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?

12-08-2005, 04:15 PM
maybe this

jverkoey
12-08-2005, 04:55 PM
What do you need help with? Fire away the questions.

YankeePride13
12-08-2005, 06:01 PM
;usersNumbers void -> void
(define (usersNumbers)
(display "Type in a number")
(display "Type in another number")
(display "Type in another number")
(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

jverkoey
12-08-2005, 06:12 PM
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:

(define (func)
(begin
....))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.

E.g:

(define (func)
(begin
(+ 3 5)
(+ 1 2)))Will output 3.

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:

(define (func)
(local [(define myvar 0)]
(begin
....)))

Also, I noticed there's a couple areas of your code that use wrong variable names (average where I think you meant avg).

YankeePride13
12-08-2005, 06:22 PM
;;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"
;

;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")
(display "Type in another number")
(display "Type in another number")
(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 :)

jverkoey
12-08-2005, 06:25 PM
Just define them with some default value, much like you'd do in another language.

C Version:
int main()
{
int a=0;
....
// Input for a here
return 0;
}

Do the same in Scheme, by keeping the original code you had, but in the local definitions assign some default value to the variables.

YankeePride13
12-08-2005, 06:32 PM
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"

;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")
(display "Type in another number")
(display "Type in another number")
(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...:/

YankeePride13
12-08-2005, 06:34 PM
never mind i realized my error in the names...now it runs but im getting odd results from the numbers im inputting...

YankeePride13
12-08-2005, 06:36 PM
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"

;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")
(display "Type in another number")
(display "Type in another number")
(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

jverkoey
12-08-2005, 06:42 PM
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.

YankeePride13
12-08-2005, 06:47 PM
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

YankeePride13
12-08-2005, 06:51 PM
anyways i figured it out myself, thanks

jverkoey
12-08-2005, 06:51 PM
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!

nickname_changed
12-08-2005, 07:34 PM
Why would any sane language insist of wrapping every statement in brackets?

JaWiB
12-08-2005, 08:13 PM
Yeah is it just due to my blind dedication to C++, or is Scheme...ugly?

jverkoey
12-08-2005, 08:33 PM
It takes some getting used to, but it can be surprisingly easy to work with compared to C syntax. There's relatively little syntax in Scheme compared to C++, so it's good to learn as a beginner's functional language.

YankeePride13
12-12-2005, 12:55 PM
Scheme is not my favorite language :) i prefer C or Java...

Rashakil Fol
12-15-2005, 04:03 AM
Scheme syntax is much more readable than C syntax. It doesn't help (a) that he didn't use code tags, and (b) if you don't use Scheme much.

(local [(define myvar 0)]

This doesn't look like Scheme to me. At least it isn't revision 5. What is this from?

jverkoey
12-15-2005, 11:19 AM
This is correct syntax in DrScheme, which is what I've been using for the past term. There's probably a few differences from the standard, I'll admit.

CornedBee
12-15-2005, 06:20 PM
I worked with Common Lisp for a bit, but I couldn't befriend the syntax - although I have to admit, it's a very powerful language.

Brian
12-17-2005, 07:05 AM
Yeah is it just due to my blind dedication to C++, or is Scheme...ugly?

scheme looks better in code tags and with a bracket matching editor such as DrScheme (http://www.drscheme.org/)

(define (nested-reverse l)
(cond
((null? l) '())
((list? (car l)) (append (nested-reverse (cdr l))
(list (nested-reverse (car l)))))
(else (append (nested-reverse (cdr l)) (list (car l))))))

This snippet reverses a list recursively. For instance,

(nested-reverse '(a b (c d (e f) (g h) i) j k))
would return
(k j (i (h g) (f e) d c) b a)

Rashakil Fol
12-17-2005, 05:31 PM
Yeah is it just due to my blind dedication to C++, or is Scheme...ugly?

If you don't like the syntax of Scheme, you could always try Haskell. It has order of operations and infix operators (gasp!). The most beautiful language out there, if you ask me.

Factorial function definition:

factorial 0 = 1
factorial n = n * factorial (n - 1)

Of course, you get to stomach things like infinite-length lists. An infinite-length list of numbers 1,2,3,...

positives = repeatup 1
where repeatup n = n : repeatup (n + 1)

CornedBee
12-18-2005, 04:16 AM
Scheme and Haskell are quite different. Scheme is still fundamentally an imperative language, although it has quite a few functional elements. Haskell is a pure functional language. It requires a different mindset to program in.

And why so complicated on the 1,2,3... thing?

positives = [1..]