Package versus namespace versus module

According to http://www.phyast.pitt.edu/~micheles/scheme/scheme29.html

“It is worth noting that if you use a package system (for example, in Common Lisp) or a namespace system (for example, in Clojure), the practice of capturing a variable becomes quite rare. Instead, in a Scheme where the system module is used, hygiene is important ... "

What is the difference between a package system, a namespace system, and a modular system because the terms are used here?

Note that this is not about Lisp -1 compared to Lisp -2 (which is a linked document separately). I guess this may have something to do with this path, in Common Lisp, trying to use the same character in two different packages, you can get two different characters with the same name.

+4
source share
2 answers

, , , . ( , , , , , ?)

Lisp , , ( , ), -, , , .

, , CL- Racket. , CL: CL , Racket Racket/Scheme.

, - , (+ a b), a b , cl:+ , - .

, CL - :

(defpackage :org.tfeb.symalg
  (:use)
  (:export "+" "-" "*" "/"))

(let ((p (find-package :org.tfeb.symalg)))
  (do-external-symbols (s (find-package :cl))
    (ecase (nth-value 1 (find-symbol (symbol-name s) p))
      ((nil)
       (import s p)
       (export s p))
      ((:external)
       nil)
      ((:inherited :internal)
       (error "package botch")))))

(defpackage :org.tfeb.symalg-user
  (:use :org.tfeb.symalg))

(, , , , : , - "", , , , .)

org.tfeb.symalg, cl, , , org.tfeb.symalg-user, cl. (+ 1 2) (org.tfeb.symalg:+ 1 2), (car '(1 . 2)) (cl:car '(1 . 2)).

(defmethod foo (a)
  (:method-combination +))

(defmethod foo (a)
  (:method-combination org.tfeb.symalg:+))

: + , cl:+. ( : org.tfeb.symalg:+, , , , .)

" " , (), .

Racket: Racket, ( ) ):

#lang racket

(provide
 (rename-out
  (plus +)
  (minus -)
  (times *)
  (divide /)))

(define (plus . args)
  (apply + args))

...

(define plus-symbol '+)

, , + plus .. - . , , (eq? '+ plus-symbol), #t: , +, .

: Racket CLOS (, , , ), + , , , .

, , CL, , - . CL Racket, .

CL :

  • - , , .
  • .
  • .
  • .

: , + org.tfeb.symalg:+, , +, org.tfeb.symalg:+, ( ), , .

Racket: , , + +. +, . , , + Racket, , , , - , . + , , .

, Scheme , -, CL " gensyms, ", Lisp -1s, , , .

+4

Lisp .

  • cl-user::create; CREATE CL-USER
  • cl-user:create; CREATE CL-USER
  • CREATE; CREATE - -
  • :create; CREATE KEYWORD
  • #:create; CREATE,

CL-USER

, CL-USER - :

(in-package "CL-USER")

cl-user::create

(defun create (x)        ;  CL-USER::CREATE
  (list :list x))

, ,

(defmacro m (x)
  `(create ,x))    ; here we use CL-USER::CREATE

, , .

(defun foo ()
  (flet ((create (x)             ; CL-USER::CREATE
           (vector :vector x)))
    (m (create 1))))             ; CL-USER::CREATE

So (m (create 1)) - (cl-user::create (cl-user::create 1)). .

BAR

FOO BAR, :

(defpackage "BAR"
  (:use "COMMON-LISP")
  (:export "FOO")
  (:import-from "CL-USER" "M"))

BAR :

(in-package "BAR")

, FOO,

(defun foo ()
  (flet ((create (x)            ;   BAR::CREATE
           (vector :vector x)))
    (m (create 1))))            ;   BAR::CREATE

So (m (create 1)) - (cl-user::create (bar::create 1)). , , .

cl-user::create:

(defun foo ()
  (flet ((cl-user::create (x)            ;   CL-USER::CREATE
           (vector :vector x)))
    (m (cl-user::create 1))))            ;   CL-USER::CREATE

, (m (create 1)) - (cl-user::create (cl-user::create 1)).

Lisp , . : #1= - , #1# .

(defun foo ()
  (flet ((#1=#:create (x)             ; #:CREATE
           (vector :vector x))) ;
    (m (#1# 1))))                     ; #:CREATE from above

So (m (#1# 1)) (cl-user::create (#:create 1)), "#: create" - .

Lisp #..

(defun foo ()
  #.(let ((create-symbol (gensym "CREATE-")))
      `(flet ((,create-symbol (x)              ; #:CREATE-NNN
                (vector :vector x))) 
         (m (,create-symbol 1)))))             ; #:CREATE-NNN from above

, :

(DEFUN FOO ()
  (FLET ((#:CREATE-815 (X)                 ; #:CREATE-815
           (VECTOR :VECTOR X)))
    (M (#:CREATE-815 1))))                 ; #:CREATE-815 from above

, (m (#:CREATE-815 1)) - (cl-user::create (#:create-815 1)), #:create-815 .

+3

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


All Articles