approximate :: (Num a, Ord a) => (a -> a) -> [(a -> a)] -> (a -> a) approximate f l = (\x -> (minF [ (h, abs $ (h x) - (f x)) | h<-l ]) x) minF :: (Num a, Ord a) => [((a->a), a)] -> (a->a) minF [] = error "lolnope" minF (p:ps) = helper (fst p) (snd p) ps where helper h minVal lst | null lst = h | snd (head lst) < minVal = helper (fst $ head lst) (snd $ head lst) (tail lst) | otherwise = helper h minVal (tail lst) iterator :: (Num a, Eq a) => (a -> a) -> [a] -> Bool iterator f l = all (\x -> f (fst x) == snd x) $ zip l (tail l) iterator' :: (Num a, Eq a) => (a -> a) -> [a] -> Bool iterator' _ [] = error "nope" iterator' f l = helper (head l) (tail l) where helper prev lst | null lst = True | f prev /= (head lst) = False | otherwise = helper (head lst) (tail lst) {- (define (maketree v l r) (list v l r)) (define (root t) (car t)) (define (left t) (cadr t)) (define (right t) (caddr t)) (define (empty? t) (null? t)) ; !!! много по-лесно с взимане на второто ниво (!) (define (children t) (if (empty? t) '() (append (if (not (empty? (left t))) (list (root (left t))) '()) (if (not (empty? (right t))) (list (root (right t))) '()) (define (grandchildren t) (append (if (not (empty? (left t))) (children (left t)) '()) (if (not (empty? (right t))) (children (right t)) '()) -} type Battery = (Int, Double) bestBattery k bs = minimum [ snd b | b<-bs, (fst b)>k ] -- (apply min (map cdr (filter (lambda (p) (> (car p) k))) bs))) quickSort' _ [] = [] quickSort' comp (x:xs) = quickSort' [ y | y<-xs, comp y x ] ++ [ y | y<-xs, not $ (comp y x)||(comp x y) ] ++ quickSort' [ y | y<-xs, comp x y ] quickSort = quickSort' (<) matrSort m = [ if odd idx then quickSort' (<) m!!idx else quickSort' (>) m!!idx | idx<-[0..(length m)-1] ] {- (define (any lst) (cond [(null? lst) #f] [(equal? (car lst) #t) #t] [else (any (cdr lst))])) ;[[1,2,3],[2,3,4,5],[3],[4,1],[5,1]] (define G '((1 2 3) (2 3 4 5) (3) (4 1) (5 1))) (define (vertices G) (map car G)) (define (neighbours v G) ( cond [(null? G) #f] [(equal? (caar G) v) (cdar G)] [else (neighbours v (cdr G))])) (define (helper k v G visited) (if (= k 0) #t (any (map (lambda (u) (helper (- k 1) u G (cons u visited))) (filter (lambda (x) (not (member x visited))) (neighbours v G)) (define (find-path k G) (any (map (lambda (v) (helper k v G '())) (vertices G))) -}