Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

RACKET (Part 1)

Name: Anonymous 2010-07-22 23:33

Racket

Name: Anonymous 2010-07-23 11:57

>>8
Easy enough in portable CL (tried to make it the same as C, including all limitations (this isn't a good idea, but I'm just faithfully implementing what you proposed), naming conventions converted to CL, so the code would look idiomatic and I don't throw away the grabber's result (more functional that way), even though I could(add a (values) form in the expansion)):


(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun symbolify (&rest symbols)
    (intern (apply #'concatenate 'string (mapcar #'string symbols)))))

(defmacro with-gensyms (names &body body)
  `(let ,(mapcar #'(lambda (name) `(,name (gensym ,(string name)))) names)
     ,@body))

(defmacro shorthand (name &key (key 'grab) (size 42) (type 'u32))
  (with-gensyms (n)
    (let ((var-name (symbolify '* name 's '*))
          (fun-name (symbolify 'grab- name)))     
      `(progn
         (defparameter ,var-name (make-array ,size :element-type ',type))
         (defun ,fun-name (,n)
           (,key (aref ,var-name ,n)))))))

(deftype u32 () '(unsigned-byte 32))

;;; define your own
(defun grab (n) (print n))

;;; Example:

(shorthand small-bomb :type t :size 10)

;;; expands to =>

(progn
  (defparameter *small-bombs* (make-array 10 :element-type 't))
  (defun grab-small-bomb (#:n1244) (grab (aref *small-bombs* #:n1244))))

;; Example usage
CL-USER> (fill *small-bombs* 'beatos)
#(BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS)
CL-USER> (grab-small-bomb 3)

BEATOS BEATOS
; the first is printed, the second is the returned value

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List