JAN
JAN

Reputation: 21895

How to define a string in Scheme - any string that I choose?

Given that:

(define output "")

or that

(define output "goodInput")

When I run those defines in my code, I get:

ERROR: In procedure memoization:
ERROR: Bad define placement (define output "").

Why is that ?

EDIT:

; Formal function of the code
(define (double->sum myString)

(define myVector 0)
(set! myVector (breaking myString))
(define output "")
(define returnValue  (checkLegit myVector)) ; check the number of legitimate characters ,they need to be either numbers or "."
(define flag 0)   
(if (not(= returnValue (vector-length myVector))) (set! output "Input error") (set! flag (+ flag 1)))

(define i 0)            ; the length of the vector
(define count 0)        ; the value of all the numbers in the vector

(if 
    (= flag 1)

(do ()                             
  ((= i (vector-length myVector))) ; run until the end of the vector
  (cond 
    ((char=? (vector-ref myVector i) #\.) ; check if we found a dot 
               (set! output (appending output count))    (set! output (appendingStrings output ".")) (set! count 0)
    )

    (else (set! count (+ count (char->integer(vector-ref myVector i))    ))  (set! count (- count 48))
    ); end of else

  ) ; end of cond

  (set! i (+ i 1))    ; inc "i" by 1
); end of do
) ; end do 

; if flag = 1 , then the input is in a correct form
(if (= flag 1) (set! output (appending output count)))

(if (= flag 1)
    output
    "Input error")
) ; END

Upvotes: 1

Views: 190

Answers (1)

Óscar López
Óscar López

Reputation: 236170

The problem is not in the string definition itself (there are no strange characters, or anything like that), it's in the place within the code where that definition is happening: you're inside a procedure, and the last line in a procedure can not be a define. Try returning something after the definition and it should work fine.

I guess that you've just started writing the procedure, just keep going after the define and write the rest of the code. For the time being, use a placeholder value at the end, so the interpreter won't complain:

(define (double->sum myString)
  (define myVector 0) 
  (set! myVector (breaking myString))
  (define output "")
  'ok)

Also a matter of style - although it's ok to define-and-set a variable like that, it's more idiomatic to use a let expression for defining local variables. This is what I mean:

(define (double->sum myString)
  (let ((myVector (breaking myString))
        (output ""))
    'ok))

In that way, you won't have to use set!, which mutates the variable and goes against the functional-programming style preferred in Scheme.

Upvotes: 1

Related Questions