ANSWER: LISP recursion using car/cdrsteemCreated with Sketch.

in common-lisp •  7 years ago 
  • Question: https://stackoverflow.com/questions/50243523
  • Snapshot:
  • See detailed step by step solution: https://github.com/sundawning/literate-programming/blob/master/english-to-nato-and-vice-verse.org
  • Why answer this homework?
    Why not.
  • Answer(There is actually no final answer, many places in the code can be improved.)
    (defun natos-englishs ()
      (list '(Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Zulu)
            '(A B C D E F G H I J K L M N O P Q R S T U V W X Z)))
    
    (defun convert-nato-english (item)
      (destructuring-bind (natos englishs)
          (natos-englishs)
        (cond ((member item natos)
               (nth (position item natos)
                    englishs))
              ((member item englishs)
               (nth (position item englishs)
                    natos))
              (t
               (error "Unvalid")))))
    
    (defun -mapcar (function list)
      (if (null list)
          nil
          (cons (funcall function
                         (car list))
                (-mapcar function
                         (cdr list)))))
    
    (defun format-lists (function lists)
      (format nil "~{~{~A~}~^ ~}"
              (-mapcar (lambda (list)
                         (-mapcar function
                                  list))
                       lists)))
    
    (defun conve (english-lists)
      (format-lists (lambda (english)
                      (string-capitalize
                       (write-to-string
                        (convert-nato-english english))))
                    english-lists))
    
    (defun convn (nato-lists)
      (format-lists (lambda (nato)
                      (write-to-string (convert-nato-english nato)
                                       :case :downcase))
                    nato-lists))  
    
    (list (conve '((P R O)
                   (L A N)))
          (convn '((Papa Romeo Oscar)
                   (Lima Alfa November))))
    
    ("PapaRomeoOscar LimaAlfaNovember" "pro lan")
    
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!