fork download
  1. ;;;10進数を2進数にしてそのnotをとって
  2. ;;;最後に10進数に戻す
  3. ;;;
  4. ;;; Scheme
  5. (define foo
  6. (lambda (n)
  7. (2to10 (notbin (10to2 n)))))
  8.  
  9. (define 2to10
  10. (lambda (lat)
  11. (cond
  12. ((null? lat) 0)
  13. ((= (car lat) 1)
  14. (+ (expt 2 (1- (length lat)))
  15. (2to10 (cdr lat))))
  16. (else
  17. (2to10 (cdr lat))))))
  18.  
  19. (define notbin
  20. (lambda (lst)
  21. (cond
  22. ((null? lst) '())
  23. ((= (car lst) 0)
  24. (cons 1 (notbin (cdr lst))))
  25. (else
  26. (cons 0 (notbin (cdr lst)))))))
  27.  
  28. (define 10to2
  29. (lambda (n)
  30. (cond
  31. ((zero? n) '())
  32. (else
  33. (orbin (cons 1 (fill 0 (log2 n)))
  34. (10to2 (- n
  35. (expt 2 (log2 n)))))))))
  36.  
  37. (define orbin
  38. (lambda (lat1 lat2)
  39. (reverse (orbin-sub (reverse lat1)
  40. (reverse lat2)))))
  41.  
  42. (define orbin-sub
  43. (lambda (lat1 lat2)
  44. (cond
  45. ((null? lat1) lat2)
  46. ((null? lat2) lat1)
  47. ((or (= 1 (car lat1))
  48. (= 1 (car lat2)))
  49. (cons 1 (orbin-sub (cdr lat1)
  50. (cdr lat2))))
  51. (else
  52. (cons 0 (orbin-sub (cdr lat1)
  53. (cdr lat2)))))))
  54.  
  55. (define fill
  56. (lambda (a n)
  57. (cond
  58. ((zero? n) '())
  59. (else
  60. (cons a (fill a (1- n)))))))
  61.  
  62. (define log2
  63. (lambda (a)
  64. (cond
  65. ((< a 2) 0)
  66. (else
  67. (1+ (log2 (div a 2)))))))
  68.  
  69. (define div
  70. (lambda (a b)
  71. (cond
  72. ((< a b) 0)
  73. (else
  74. (1+ (div (- a b) b))))))
  75.  
Success #stdin #stdout 0.02s 4132KB
stdin
Standard input is empty
stdout
Standard output is empty