Advertisement
MarkUa

Untitled

Sep 17th, 2019
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. (define (count_ lst len)
  2.  
  3. (cond ((EQV? lst '() ) len )
  4. ((not_ (PAIR? lst)) len )
  5. ( (= 1 1) (
  6. count_ (cdr lst ) (+ len 1)
  7. ))))
  8. ; reverse list
  9. (define (reverse_ list_ result position )
  10. (define len ( count_ list_ 0) )
  11. (cond ((= len 0) result )
  12. ( (and_ (= position 1 ) (and_ (PAIR? list_) (LIST? list_)) )
  13.  
  14. (cons (car list_) result ) )
  15. ( (and_ (= position 1 ) (and_ (PAIR? list_) (not_ (LIST? list_))) )
  16.  
  17. (cons list_ result ) )
  18. ( (= 1 1)
  19. (reverse_ (cdr list_) (cons (car list_) result ) ( - position 1) )
  20. )
  21. )
  22. )
  23. ; check second part for different issues
  24. (define (check li copy possible_pair_position deep)
  25. (define len ( count_ li 0) )
  26. ; (display len)
  27. (cond ((= len 0 ) copy )
  28. ( (and_ (= possible_pair_position 1 ) (and_ (PAIR? li) (LIST? li)) )
  29.  
  30. (reverse_ (cons (car li) copy ) '() deep) )
  31. ( (and_ (and_ (= possible_pair_position 1 ) (PAIR? li)) (not_ (LIST? li)) )
  32.  
  33. (reverse_ (cons li copy ) '() deep) )
  34. ( (= possible_pair_position 1 )
  35.  
  36. (cons (car li) copy ) )
  37. ((= 1 1)
  38.  
  39. (check (cdr li ) (cons (car li) copy ) (- possible_pair_position 1) deep )
  40. )
  41. )
  42.  
  43. )
  44. ;second part formatting
  45. (define (second my_list)
  46. (define len ( count_ my_list 0) )
  47. (cond ((< len 2) "less than 2")
  48. ((= len 2) '() )
  49. ((= 1 1) ( check (cddr my_list) '() (- len 2) (- len 2) )
  50. )
  51. )
  52.  
  53. )
  54.  
  55. ;first part formatting
  56. (define (first my_list)
  57.  
  58. (cond ((and_ (not_ (LIST? my_list)) (not_ (PAIR? my_list))) "atom" )
  59. ((< ( count_ my_list 0) 2) "list size less than 2" )
  60. ((= ( count_ my_list 0) 2)
  61. (cond ((and_ (not_ (LIST? my_list)) (PAIR? my_list) )
  62. (cons (car my_list) (cons ( cdr my_list) '()))
  63.  
  64. )
  65. ((= 1 1)
  66. (cons (car my_list) (cons ( cadr my_list) '() ))
  67. )
  68. ) )
  69. ((> ( count_ my_list 0) 2)
  70. (cons (car my_list) (cons ( cadr my_list) '() ) )
  71.  
  72. )
  73.  
  74. ((= 1 1) '(car my_list)
  75.  
  76. )
  77. )
  78.  
  79.  
  80. )
  81. (define (combine_parts first_part x)
  82.  
  83. (cond ( (EQ? first_part "atom" )
  84.  
  85. "atom"
  86. )
  87. ((EQ? first_part "list size less than 2") "list size less than 2" )
  88. ((= 1 1)
  89.  
  90. (cons first_part (cons (second x) '() ))
  91.  
  92. )
  93. )
  94.  
  95.  
  96. )
  97. (define object_to_process '( 2 44 5 . 3 ) )
  98.  
  99. (display object_to_process)
  100. (display "\n")
  101.  
  102. (combine_parts (first object_to_process) object_to_process)
  103.  
  104. (define object_to_process1 '( (2 44) 5 . 3 ) )
  105. (display object_to_process1)
  106. (display "\n")
  107.  
  108. (combine_parts (first object_to_process1) object_to_process1)
  109.  
  110. (define object_to_process2 '( 2 44 5 2 4 5 . 3 ) )
  111.  
  112. (display object_to_process2)
  113. (display "\n")
  114.  
  115. (combine_parts (first object_to_process2) object_to_process2)
  116.  
  117. (define object_to_process3 '( 2 44 ) )
  118.  
  119. (display object_to_process3)
  120. (display "\n")
  121.  
  122. (combine_parts (first object_to_process3) object_to_process3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement