Solution of Baris Guldali


(define (spell r) ;this a helper function which converts r into spelling
 (item ( + 1 r) `( "" ein zwei drei vier funf sechs sieben acht neun)))
;these are helper-functions which point the several digits
  (define (tausend n) (- (count n) 3))
     (define (hundert n) (- (count n) 2))     
        (define (zehn n)    (- (count n) 1))
            (define (einer n)   (last n))
;these are again helper-functions which read the digits
  (define (t n) (item (tausend n) n))
      (define (h n) (item (hundert n) n))
          (define (z n) (item (zehn n) n))
              (define (e n) (item (einer n) n))
(define (convert n) ;this is the main function
  (cond ((not (integer? n)) `(The argument is not an integer))
        ((or (> n 9999) (< n 0))   
           `(In school we have not seen yet how to express this))
(else
 (word         
   (if (< (tausend n) 1) ;controls whether there is any tausend
     ""  (word (spell (t n)) `tausend))
   (if (< (hundert n) 1) ;controls whether there is any hundert
     "" (if (= (h n) 0) "" ;if hund exist&is 0 then blanc 
            (word (spell (h n)) `hundert)))
;let`s have a look to the ten`s
   (if
       (or (< (zehn n) 1) (= (z n) 0)) ;ten digit mustn`t exist or must be 0
         (cond ((= n 0) `null)
                ((= (einer n) 0) "")
                 ((= (einer n) 1) `eins) ;exception
                   (else (spell (einer n)))
         )
         (if (= (z n) 1)   ;as you know 10..19 are slightly different
              (cond ((= (einer n) 1 ) `elf) ;here are some exceptions
                      ((= (einer n) 2) `zwolf)
                        ((= (einer n) 0) `zehn)
                          (else (word
                             (cond ((= (einer n) 7) `sieb)  
                                    ((= (einer n) 6) `sech)
                                      (else (spell (einer n))))
                         `zehn  )       
                          )
              
              )
              (word
                   (if (= (einer n) 0) "" (word (spell (einer n)) `und))
                   (if (= (z n) 3) `dreissig
                       (word 
                           (cond ((= (z n) 7) `sieb)   ;in german one digit
                                  ((= (z n) 6) `sech)  ;is read before ten
                                   ((= (z n) 2) `zwan) ;exceptions
                                      (else (spell (z n)))
                           )
                         `zig
                       ) ;word added zwan+zig,neun+zig etc.
                   )
              )
          )
    )
))))