Search code examples
lispracketprimitivebuilt-in

Primitives and built-in functions in Racket


Are primitives and built-in functions the same thing in Racket? If no, what's the difference between them?


Solution

  • The short answer is no!

    A primitive function are those which aren't implemented in its own runtime but inherently implemented by the runtime. eg. cons is implemented in C and the racket VM comes with the code that implements it. list* too, but it didn't really need to be a primitive. Some primitives are so just to speed things up.

    Built in functions just means they come with the language. These are all primitives and all the standard library that is implemented in the language itself and is shipped with the implementation. An example is make-list. If you rigth click in the IDE and choose "Open defining file" you'll see it's implementation in racket:

    (define (make-list n x)
      (unless (exact-nonnegative-integer? n)
        (raise-argument-error 'make-list "exact-nonnegative-integer?" n))
      (let loop ([n n] [r '()])
        (if (zero? n) r (loop (sub1 n) (cons x r)))))
    

    An example of function that are neither primitive nor built in would be many of the packages in pkgs.racket-lang.org.