Practical circuit programming

It has been several months since I touched Schema and decided to implement a revenue splitter on the command line using Scheme.

My initial implementation used simple recursion to continue, but I decided that the continuation would be more suitable for this type of program. I would appreciate it if someone (more experienced with the Scheme than me) could take a look at this and suggest improvements. I believe that a few lines (display...is an ideal opportunity to use a macro (so far I have not received macros yet).

(define (ab-income)
  (call/cc
   (lambda (cc)
     (let
         ((out (display "Income: "))
          (income (string->number (read-line))))
       (cond
         ((<= income 600)
          (display (format "Please enter an amount greater than $600.00~n~n"))
          (cc (ab-income)))
         (else
          (let
              ((bills    (* (/ 30 100) income))
               (taxes    (* (/ 20 100) income))
               (savings  (* (/ 10 100) income))
               (checking (* (/ 40 100) income)))
            (display (format "~nDeduct for bills:---------------------- $~a~n" (real->decimal-string bills 2)))
            (display (format "Deduct for taxes:---------------------- $~a~n" (real->decimal-string taxes 2)))
            (display (format "Deduct for savings:-------------------- $~a~n" (real->decimal-string savings 2)))
            (display (format "Remainder for checking:---------------- $~a~n" (real->decimal-string checking 2))))))))))

(ab-income) , - 600, ( ) (ab-income) current-continuation. ( ) . , , (ab-income), 600, .

(, , !)

+5
1

, . , . - , ; , , . , ; , , , . , . :

(define (some-function x y)
  (preprocess x)
  (combine (modified x) y))
(some-function alpha beta)

some-function, : , .. (preprocess x). some-function , some-function, preprocess. , preprocess . modified; , modified , combine. , , combine, some-functionsome-function , ! , , ; combine , . (combine (modified x) y) - , .

, :

(define (my-while cond body)
  (when (cond)
    (body)
    (my-while cond body)))

(let ((i 0))
  (my-while (lambda () (< i 10))
            (lambda () (display i) (newline) (set! i (+ i 1)))))

, my-while. , , my-while cond body , , .

-, . display, . - : define, , .. , s- , " ". , , - , .

, , :

(require (lib "string.ss"))

(define (print-report width . nvs)
  (if (null? nvs)
    (void)
    (let ((name  (car  nvs))
          (value (cadr nvs)))
      (display (format "~a:~a $~a~n"
                       name
                       (make-string (- width (string-length name) 2) #\-)
                       (real->decimal-string value 2)))
      (apply print-report width (cddr nvs)))))

(define (ab-income)
  (display "Income: ")
  (let ((income (string->number (read-line))))
    (if (or (not income) (<= income 600)) 
      (begin (display "Please enter an amount greater than $600.00\n\n")
             (ab-income))
      (begin (newline)
             (print-report 40 "Deduct for bills"       (* 3/10 income)
                              "Deduct for taxes"       (* 2/10 income)
                              "Deduct for savings"     (* 1/10 income)
                              "Remainder for checking" (* 4/10 income))))))

-, , mzscheme (require (lib "string.ss")) real->decimal-string. display, . , 40- , . , print-report. - ; 40. - . ( ) , , . format display . ( , ).

(display "Income: ") let; , ? if, , input false, , string->number . , , , , , . (, , print-report display format s.)

, ; , , .

+17

All Articles