Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define (count1 lst len)
- (cond ((null? lst) len )
- ((not (PAIR? lst)) len )
- (else (
- count1 (cdr lst ) (+ len 1)
- ))))
- (define (reverse_ list_ result position )
- (cond ((null? list_) result )
- ( (and (= position 1 ) (PAIR? list_) (LIST? list_) )
- (cons (car list_) result ) )
- ( (and (= position 1 ) (PAIR? list_) (not (LIST? list_)) )
- (cons list_ result ) )
- (else
- (reverse_ (cdr list_) (cons (car list_) result ) ( - position 1) )
- )
- )
- )
- (define (check li copy possible_pair_position deep)
- (cond ((null? li) copy )
- ( (and (= possible_pair_position 1 ) (PAIR? li) (LIST? li) )
- (reverse_ (cons (car li) copy ) '() deep) )
- ( (and (= possible_pair_position 1 ) (PAIR? li) (not (LIST? li)) )
- (reverse_ (cons li copy ) '() deep) )
- ( (= possible_pair_position 1 )
- (cons (car li) copy ) )
- (else
- (check (cdr li ) (cons (car li) copy ) (- possible_pair_position 1) deep )
- )
- )
- )
- (define (second my_list)
- (define len ( count1 my_list 0) )
- (cond ((< len 2) "less than 2")
- ((= len 2) '() )
- (else ( check (cddr my_list) '() (- len 2) (- len 2) )
- )
- )
- )
- (define r '() )
- (define (first my_list)
- (cond ((and (not (LIST? my_list)) (not (PAIR? my_list))) "atom" )
- ((< ( count1 my_list 0) 2) "list size less than 2" )
- ((= ( count1 my_list 0) 2)
- (cond ((and (not (LIST? my_list)) (PAIR? x) )
- (set! r (cons ( cdr my_list) r))
- (set! r (cons (car my_list) r))
- r )
- (else
- (set! r (cons ( cadr my_list) r))
- (set! r (cons (car my_list) r))
- r )
- ) )
- ((> ( count1 my_list 0) 2)
- (set! r (cons ( cadr my_list) r))
- (set! r (cons (car my_list) r))
- r )
- (else '(car my_list)
- )
- )
- )
- (define x '( 2 44 5 . 3 ) )
- (display x)
- (display "\n")
- (define first_part (first x))
- (define second_part '() )
- (define result '())
- (cond ( (or (EQ? first_part "atom" ) (EQ? first_part "list size less than 2"))
- (display "could not make oparations because of ")
- (display first_part) )
- (else
- (set! second_part (second x))
- (set! result (cons second_part result))
- (set! result (cons first_part result))
- (display result)
- )
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement