Scheme

This is a discussion on Scheme within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm having trouble with my scheme program and I was wondering if anyone knew any boards that dealt with the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    Scheme

    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?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    What do you need help with? Fire away the questions.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    ;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

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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:

    Code:
    (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:
    Code:
    (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:

    Code:
    (define (func)
      (local [(define myvar 0)]
        (begin
           (set! myvar (readTillNum))
           ....)))
    Also, I noticed there's a couple areas of your code that use wrong variable names (average where I think you meant avg).

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    ;;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

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Just define them with some default value, much like you'd do in another language.

    C Version:
    Code:
    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.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    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...:/

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    never mind i realized my error in the names...now it runs but im getting odd results from the numbers im inputting...

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    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

  11. #11
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    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

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    36
    anyways i figured it out myself, thanks

  14. #14
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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!

  15. #15
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Why would any sane language insist of wrapping every statement in brackets?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help w/inheritance scheme
    By dudeomanodude in forum C++ Programming
    Replies: 10
    Last Post: 11-30-2008, 12:53 PM
  2. Apple's music protection scheme is hacked
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-27-2006, 05:33 PM
  3. Naming Scheme...
    By SgtMuffles in forum Windows Programming
    Replies: 6
    Last Post: 06-13-2006, 11:45 PM
  4. ping pong buffering scheme
    By cblix in forum Tech Board
    Replies: 0
    Last Post: 11-23-2005, 05:26 PM
  5. Post your IDE scheme!
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-09-2005, 06:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21