Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define (foldl l nv op)
- (if (null? l)
- nv
- (foldl (cdr l) (op nv (car l)) op)))
- (define (foldr l nv op)
- (if (null? l)
- nv
- (op (car l) (foldr (cdr l) nv op))))
- (define (acc a b nv op term next)
- (if (> a b)
- nv
- (op (term a) (acc (next a) b nv op term next))))
- (define (acc-i a b nv op term next)
- (if (> a b)
- nv
- (acc-i (next a) b (op nv (term a)) op term next)))
- ;Task3
- (define (numGame l)
- (define (last-digit x) (remainder x 10))
- (define (first-digit x)
- (if (< x 10)
- x
- (first-digit (quotient x 10))))
- (or (null? l)
- (not (not (foldl (cdr l) (car l) (lambda (x y)
- (if (or (eqv? x #f) (not (= (last-digit x) (first-digit y))))
- #f
- y)))))))
- ;Task4
- (define (generate a b l)
- (define square (lambda (x) (* x x)))
- (acc a
- b
- '()
- (lambda (x y) (if (member (square x) l) (cons x y) y))
- (lambda (x) x)
- (lambda (x) (+ 1 x))))
- ;Task5
- (define (largestInterval f g a b)
- (define (cut-first-n l n)
- (if (or (null? l) (= n 0))
- l
- (cut-first-n (cdr l) (- n 1))))
- ;Assume that l is not null
- (define (get-same-int l)
- (define (find-whole l)
- (if (or (null? (cdr l)) (not (eqv? (caar l) (caadr l))))
- (cdar l)
- (find-whole (cdr l))))
- (cons (cdar l) (find-whole l)))
- (define (int-len int)
- (+ 1 (- (cdr int) (car int))))
- (define (find-longest int longest)
- (cond ((null? int) longest)
- ((eqv? #f (caar int)) (find-longest (cdr int) longest))
- (else (let ((curr (get-same-int int)))
- (find-longest (cut-first-n int (int-len curr))
- (if (> (int-len curr) (int-len longest)) curr longest))))))
- (let* ((list-interval (acc a b '() cons (lambda (x) x) (lambda (x) (+ 1 x))))
- (result-f (map f list-interval))
- (result-g (map g list-interval))
- (same-pairs (map cons (map (lambda (x y) (= x y)) result-f result-g) list-interval)))
- (find-longest same-pairs (cons 0 0))))
- ;Task3 - 2018
- (define (meetTwice? f g a b)
- (>= (acc-i a
- b
- 0
- (lambda (x y) (if (= (f a) (g a)) (+ x 1) x))
- (lambda (x) x)
- (lambda (x) (+ 1 x))) 2))
- ;Task4 - 2018
- (define (next-look-and-say seq)
- (define (count-same l x)
- (if (or (null? l) (not (= x (car l))))
- 0
- (+ 1 (count-same (cdr l) x))))
- (if (null? seq)
- '()
- (let ((same-len (count-same seq (car seq))))
- (cons same-len (cons (car seq) (next-look-and-say (list-tail seq same-len)))))))
- (define test (lambda l (length l)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement