What is the time complexity of the assoc function in a schema?

What is the time complexity of this "excellent" function?

+3
source share
1 answer

I would suggest that assoc- O (n) *, assuming that equal?is O (1) in your function use. This is because it is trivial to write your own version assoc:

(define (my-assoc v lst)
  (cond ((null? lst) #f)
        ((equal? v (caar lst)) (car lst))
        (else (my-assoc v (cdr lst)))))

You can see that this simply reduces the list lstuntil a match is found. If none are found, returns #f.

* technically equal? - O (n), n - , , assoc, O (n * m), n - , assoc, m - v.

+6

Source: https://habr.com/ru/post/1711130/


All Articles