Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define btr-sum
- (lambda(a b)
- (cond
- ((< (string-length a)(string-length b))(btr-sum (normalize a b) b))
- ((> (string-length a)(string-length b))(btr-sum a (normalize b a)))
- ((and(char=? (string-ref a 0) #\. )(char=?(string-ref b 0)#\b))(btr-sum (substring a 1 )(substring b 1)))
- (else (real (normalize(real a b ".") (create-rip a b ".")) (create-rip a b ".") "."));; (+(+ a b) c)
- )
- )
- )
- (define real ;;sums strings a and b without carry
- (lambda(a b r);;strings
- (if (>(string-length a)1)
- (string-append
- (real ;;recursive
- (substring a 0 (-(string-length a)1))
- (substring b 0 (- (string-length b)1))
- r)
- (string(somma ;;sums last chars of strings
- (string-ref a (-(string-length a)1))
- (string-ref b (-(string-length b)1))
- )
- )
- )
- (string(somma (string-ref a 0)(string-ref b 0)))
- )
- )
- )
- (define normalize ;;if the length of subj < length of obj, normalize fills stacks up subj with "."
- (lambda(subj obj)
- (if (not(= (string-length subj)(string-length obj)))
- (normalize (string-append "." subj) obj)
- subj
- )
- )
- )
- (define create-rip ;;generates the carry of the sum of a and b
- (lambda (a b c)
- (if (> (string-length a) 1)
- (cond ;; if length>1 then do it again
- (
- (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))));;if +/+, add one + in carry
- (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "+" c))
- )
- (
- (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))if -/-, add one - in carry
- (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "-" c))
- )
- (else (create-rip (substring a 0 (-(string-length a)1))(substring b 0 (-(string-length b)1))(string-append "." c)))
- )
- (cond ;;if it's the last char do it only once
- (
- (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))))
- (string-append "+" c)
- )
- (
- (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))))
- (string-append "-" c)
- )
- (else (string-append "." c))
- )
- )
- )
- )
- (define somma ;;general procedure to sum two char without taking care of carry
- (lambda(a b)
- (cond
- ((and (char=? a #\-)(char=? b #\-))#\+)
- ((and (char=? a #\.) (char=? b #\-))#\-)
- ((and (char=? a #\+)(char=? b #\-))#\.)
- ((and (char=? a #\-)(char=? b #\+))#\.)
- ((and (char=? a #\.)(char=? b #\+))#\+)
- ((and (char=? a #\+)(char=? b #\+))#\-)
- ((and (char=? a #\-)(char=? b #\.))#\-)
- ((and (char=? a #\.)(char=? b #\.))#\.)
- ((and (char=? a #\+)(char=? b #\.)) #\+)
- )
- )
- )
- (btr-sum "-""--")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement