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 ;;fa sommare prima a e b e poi c
- (lambda(a b r);;stringa stringa stringa
- (if (>(string-length a)1)
- (string-append
- (real ;;parte ricorsiva
- (substring a 0 (-(string-length a)1))
- (substring b 0 (- (string-length b)1))
- r)
- (string(somma ;;somma ultimi caratteri
- (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
- (lambda(subj obj)
- (if (not(= (string-length subj)(string-length obj)))
- (normalize (string-append "." subj) obj)
- subj
- )
- )
- );; normalize : rende la prima stringa lunga come la seconda
- (define create-rip ;;crea il riporto dalla somma di a e b
- (lambda (a b c)
- (if (> (string-length a) 1)
- (cond ;;se è rimasto più di un carattere, iterativo
- (
- (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))));;se sono entrambi ++ metti un + nel riporto
- (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))));;se sono entrambi -- metti un - nel riporto
- (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 ;;se è rimasto un carattere non iterare, unisci l'ultimo riporto a c e restituisci la c
- (
- (and (char=? #\+(string-ref a (-(string-length a)1)))(char=? #\+ (string-ref b (-(string-length b)1))));;se sono entrambi ++ metti un + nel riporto
- (string-append "+" c)
- )
- (
- (and (char=? #\-(string-ref a (-(string-length a)1)))(char=? #\- (string-ref b (-(string-length b)1))));;se sono entrambi -- metti un - nel riporto
- (string-append "-" c)
- )
- (else (string-append "." c))
- )
- )
- )
- )
- (define somma ;;somma due caratteri
- (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