Advertisement
MarkUa

Untitled

Sep 17th, 2019
2,956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; list length
  2. (define (count_ lst len)
  3.  (cond ((null? lst) len  )
  4.         ((not (PAIR? lst)) len )
  5.         (else  (
  6.                     count_ (cdr lst  ) (+ len 1)
  7.                           ))))
  8. ; reverse list
  9. (define (reverse_ list_  result  position )
  10.  
  11.     (cond ((null? list_)  result  )
  12.           ( (and (= position 1  ) (PAIR?  list_) (LIST? list_) )
  13.  
  14.                             (cons  (car list_)  result ) )
  15.              ( (and  (= position 1  ) (PAIR?  list_) (not (LIST? list_)) )
  16.  
  17.                           (cons   list_   result ) )
  18.              (else
  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.       (cond ((null? li)  copy  )
  26.             ( (and (=  possible_pair_position 1  ) (PAIR?  li) (LIST? li) )
  27.  
  28.                          (reverse_ (cons  (car li)  copy ) '() deep)     )
  29.              ( (and (=  possible_pair_position 1  ) (PAIR?  li) (not (LIST? li)) )
  30.  
  31.                          (reverse_ (cons   li   copy ) '() deep)  )
  32.            ( (=  possible_pair_position 1  )
  33.  
  34.              (cons  (car li) copy ) )
  35.            (else
  36.                
  37.       (check (cdr li  ) (cons (car li) copy )   (- possible_pair_position 1) deep )                
  38.             )
  39.        )
  40.  
  41.  )
  42. ;second part formatting
  43. (define (second my_list)
  44.      (define len  ( count_ my_list 0) )
  45.      (cond ((< len 2) "less than 2")
  46.            ((= len 2) '() )
  47.            (else  ( check (cddr my_list) '()  (- len 2) (- len 2) )
  48.                            )
  49.            )
  50.        
  51.   )
  52. ;first part formatting
  53. (define (first my_list)
  54.    
  55.      (cond ((and  (not (LIST? my_list)) (not (PAIR? my_list))) "atom" )
  56.            ((< ( count_ my_list 0) 2) "list size less than 2"  )
  57.            ((= ( count_ my_list 0) 2)
  58.                                        (cond ((and (not (LIST? my_list)) (PAIR? x) )
  59.                                               (cons (car my_list) (cons ( cdr my_list) '()))
  60.  
  61.                                               )
  62.                                        (else
  63.                                                (cons (car my_list) (cons ( cadr my_list) '() ))
  64.                                                )
  65.                                              )                  )
  66.            ((> ( count_ my_list 0) 2)
  67.                                     (cons (car my_list) (cons ( cadr my_list) '() ) )
  68.             )
  69.                     (else  '(car my_list)
  70.                            )
  71.            )
  72.      
  73.      
  74.   )
  75. (define (combine_parts  first_part x)
  76. (cond ( (or (EQ? first_part "atom" ) (EQ? first_part "list size less than 2"))
  77.          (display "could not make oparations because of ")
  78.         (display first_part)  )
  79.       (else                            
  80.               (cons first_part (cons  (second x) '() ))
  81.        )
  82. )
  83.   )
  84.  (define object_to_process '( 2 44 5 . 3  ) )
  85.  (display object_to_process)
  86. (display "\n")
  87. (combine_parts  (first  object_to_process) object_to_process)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement