>From the expansion result, match does check whether the inspected
value is a list using list?. But at least for the current version of
Racket (and the C VM), list? is handled specially and ``effectively
takes constant time due to internal caching.'' (quoted from:
https://docs.racket-lang.org/reference/pairs.html?q#(def._((quote._~23~25kernel)._list~3f))
)

On Tue, Oct 29, 2019 at 12:41 PM Christopher Lemmer Webber
<cweb...@dustycloud.org> wrote:
>
> Imagine the following code:
>
> (let lp ([items '(1 2 3 4 5)])
>   (match items
>     [(list head rest ...)
>      (cons (* head 2)
>            (lp rest))]))
>
> My gut feeling is "oh, this is just O(n) because it's pulling the top
> off the list.... quite efficient."
>
> But then I realized that:
>
> (match '(1 2 3 4 . 5)
>   [(list head rest ...)
>    (* head 2)])
>
> throws an exception because it's a dotted list.
>
> But the documentation says that the `list?` predicate is O(n).  I'm
> suddenly fearing, have I been writing O(n^2) code this whole time?  Does
> match against list actually check the whole list every time?
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/87lft3xxdo.fsf%40dustycloud.org.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAMTzy%2BZ6z6EETExv-5QaDdKy-nG%3DVF%2BC3N4w0VoMNwLL%3D0gikA%40mail.gmail.com.

Reply via email to