Writing to a file in a schema

The goal is to check if the received character is a number or an operand, and then print it to a list that will be written to the txt file. I am wondering which process will be more efficient, how to do it, as I said above (write it to a list and then write this list to a file) or write to a txt file directly from the procedure. I am new to the scheme, so I apologize if I do not use the correct terminology

(define input '("3" "+" "4"))

(define check
   (if (number? (car input))
    (write this out to a list or directly to a file)
    (check the rest of file)))

Another question that I had in mind, how can I make the validation process recursive? I know this is a lot, but I'm a little disappointed in checking the methods I found on other sites. I really appreciate the help!

+3
source share
1 answer

, , . , ( - ), :

(define (check input)
  (cond ((null? input) ; the input list is empty
         <???>)        ; return the empty list
        ((string->number (car input)) ; can we convert the string to a number?
         (cons "number"  <???>)) ; add "number" to list, advance the recursion
        (else                    ; the string was not a number
         (cons "operand" <???>)))) ; add "operand" to list, advance recursion

:

(define (write-to-file path string-list)
  (call-with-output-file path
    (lambda (output-port)
      (write <???> output-port)))) ; how do you want to write string-list ?

, lambda, , , , , , , .. , :

(write-to-file "/path/to/output-file"
               (check input))
+2

All Articles