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

Pages: 1-

c++ exercise

Name: Anonymous 2008-03-06 13:16

Sup /prog/
I am looking for a whole lot of c++ exercises at mediocore difficulty level. Does /prog/ know a good site with lots of these?

Name: Anonymous 2008-03-06 13:20

Name: Anonymous 2008-03-06 13:31

Name: Anonymous 2008-03-06 13:39

Check ``The Little Seppler

Name: Anonymous 2008-03-06 13:52

''

Name: Anonymous 2008-03-06 17:23

Name: Anonymous 2008-03-06 19:26

>>3
>>6
Thanks <3

Name: Anonymous 2008-03-07 0:08

Name: Just Testing, Ignore. 2010-04-22 5:36

;;; Call stack access functionality library for Common Lisp

;;; The standard CL functions DEFUN LAMBDA LABELS FLET are redefined
;;; in such a way that there would be a dynamic variable *call-stack*
;;; and a symbol macro THIS-FUNCTION accessible.

;;; *CALL-STACK* represents the current call stack, it's a list/stack
;;; of FUNCTION-WRAPPER objects. A FUNCTION-WRAPPER object contains
;;; the function name, lambda list, arguments, the caller and the
;;; current function object/closure.

;;; THIS-FUNCTION is the current FUNCTION-WRAPPER object representing
;;; the current function object/closure.

;;; For convenience reasons FUNCALL APPLY MULTIPLE-VALUE-CALL are
;;; redefined as well, so you can FUNCALL/APPLY/M-V-C FUNCTION-WRAPPER
;;; objects. If you want to get to the original function object
;;; manually, just call function-object.

;;; Current issues:
;;;
;;; This implementation probably breaks lots of optimizations which
;;; can be done on arguments.  Some optimizations may be possible by
;;; declaring *CALL-STACK* as having DYNAMIC-EXTENT, but that might
;;; lead to some problems in certain usage cases, so I haven't
;;; included that declaration.

;;; TCO in functions using these library is probably broken as well as
;;; we're keeping our own call stack.  Some implementations may be
;;; able to do TCO in the presence of dynamic variables, but I haven't
;;; looked into what implementations can actually do that.

;;; Standard lambda lists are treated as destructuring-bind lists,
;;; which support a bit more options than standard lambda lists.
;;; FLET/LABELS implementation could be slightly more efficient (avoid
;;; the APPLY call).

;;; In closing: this library is rather alpha-quality, although it
;;; works fine. Feel free to spend more time to polish it if you want,
;;; but I've only implemented it to show how it can be done. I've
;;; never actually felt the need for such a library when writing code.
;;; Such functionality isn't in CL because it would add some overhead
;;; to compiled lisps, although if you want it, it's easy to
;;; implement, as I've shown below.  In interpreted Lisps, it may
;;; sometimes be present: I think you can do this without overhead in
;;; CLISP by getting the current environment object and examining it.

;;; This is supposed to be split into 3 files and managed by ASDF,
;;; some other defsystem or compile/loaded manually:
;;;
;;; utils.lisp, fun-arguments.lisp, examples.lisp (loaded in that order)

;;; It will however work fine as a single file due to layout/eval-when's.

Name: Just Testing, Ignore. 2010-04-22 5:38

;;; utils.lisp:

(cl:defpackage #:various-utils
  (:use #:cl)
  (:export #:with-gensyms #:meta #:parse-body))

(in-package #:various-utils)

(defmacro with-gensyms (syms &body body)
  `(let ,(mapcar #'(cl:lambda (sym) `(,sym (gensym ,(string sym)))) syms)
     ,@body))

(defmacro meta (&body forms)
  (let ((self (gensym)))
    `(macrolet ((,self () ,@forms))
       (,self))))

;; PARSE-BODY is taken from the Alexandria library.
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun parse-body (body &key documentation whole)
    "Parses BODY into (values remaining-forms declarations doc-string).
Documentation strings are recognized only if DOCUMENTATION is true.
Syntax errors in body are signalled and WHOLE is used in the signal
arguments when given."
    (let ((doc nil)
          (decls nil)
          (current nil))
      (tagbody
       :declarations
         (setf current (car body))
         (when (and documentation (stringp current) (cdr body))
           (if doc
               (error "Too many documentation strings in ~S." (or whole body))
               (setf doc (pop body)))
           (go :declarations))
         (when (and (listp current) (eql (first current) 'declare))
           (push (pop body) decls)
           (go :declarations)))
      (values body (nreverse decls) doc))))

Name: Just Testing, Ignore. 2010-04-22 5:40

;;; fun-arguments.lisp:

(eval-when (:compile-toplevel :load-toplevel :execute)
  (various-utils:meta
    `(progn
       (defpackage #:function-arguments
         (:nicknames #:fun-args)
         (:use #:cl #:various-utils)
         (:shadow
          ,@#1='(#:defun #:lambda #:labels #:flet
                 #:funcall #:apply #:multiple-value-call))
         (:export
          ,@#1#
          #:*call-stack* #:this-function
          #:function-wrapper
          #:function-name
          #:function-lambda-list
          #:arguments
          #:function-object
          #:caller
          #:+shadowed-exports+
          #:*print-function-object-long-form*))
       ;; Terrible defparameter emulating hack (needed since the package
       ;; does not exist at readtime yet)
       (let ((shadowed-exports
              (find-symbol (string '#:+shadowed-exports+)
                           (find-package '#:function-arguments))))      
         (setf (symbol-value shadowed-exports) #1#)
         (proclaim `(special ,shadowed-exports))))))

(cl:in-package #:fun-args)

(defparameter *unbound-marker* (gensym "SLOT-UNBOUND"))
 
(defclass function-wrapper ()
  ((name        :initarg :name        :reader function-name       
                :initform *unbound-marker*)  
   (lambda-list :initarg :lambda-list :reader function-lambda-list)
   (arguments   :initarg :arguments   :reader arguments)
   (object      :initarg :object      :reader function-object     
                :initform nil)
   (caller      :initarg :caller      :reader caller)))

(defvar *print-function-object-long-form* t
  "Controls if a long form containg the wrapped function object
 will be printed, or a short form containing the name")

(defmethod print-object ((object function-wrapper) stream)
  (print-unreadable-object (object stream :type t :identity t)
    (let ((name (function-name object)))     
      (if (or *print-function-object-long-form* (eql name *unbound-marker*))
          (princ (function-object object) stream)       
          (princ name stream)))))

(defvar *call-stack* nil
  "Call stack of function wrapper objects.")

(eval-when (:compile-toplevel :load-toplevel :execute)
  (cl:defun generate-wrapper-function-body (anonymousp lambda-list body
                                            &key (name nil name-present-p)
                                            object)
    (multiple-value-bind (body declarations doc-string)
        (parse-body body :documentation t)     
      (with-gensyms (rest)  
        `(,(if anonymousp 'cl:lambda name) (&rest ,rest)
           ,@(when doc-string `(,doc-string))          
           (symbol-macrolet ((this-function #1=(first *call-stack*)))
             ;; the right way is to use a simple lambda-list
             ;; destructuring function, but this works too, although
             ;; it supports more patterns than DEFUN does.
             (destructuring-bind ,lambda-list ,rest
               ,@declarations
               (let ((*call-stack*
                      (cons
                       (make-instance 'function-wrapper
                                      ,@(when name-present-p
                                              `(:name ',name))
                                      :arguments ,rest
                                      :lambda-list ',lambda-list
                                      :object ,(if anonymousp
                                                   object
                                                   `#',name)
                                      ;; I think caller is pointless
                                      ;; when you can just look these
                                      ;; objects up the *call-stack*,
                                      ;; but for the sake of the example:
                                      :caller #1#)
                       *call-stack*)))                
                 ,@body)))))))
  ;; This can be defined in a more straightforward way using LABELS,
  ;; but I did it this way to show that it's still possible even
  ;; without LABELS.
  (cl:defun generate-wrapper-lambda-body
      (lambda-list body &key (name nil name-present-p))
    (with-gensyms (the-lambda)
      `(let (,the-lambda)
         (setf ,the-lambda
               #',(cl:apply #'generate-wrapper-function-body t lambda-list body
                            :object the-lambda
                            (when name-present-p `(:name ,name))))))))

(defmacro defun (name lambda-list &body body)  
  `(cl:defun ,@(generate-wrapper-function-body
                nil lambda-list body :name name)))

(defmacro lambda (lambda-list &body body)
  (generate-wrapper-lambda-body lambda-list body))

(macrolet
  ((define-function-definer (name original-name)
     `(defmacro ,name (definitions &body body)         
        `(,',original-name
          ,(mapcar #'(cl:lambda (definition)
                       (with-gensyms (rest)
                         (destructuring-bind (name lambda-list &rest the-body)
                             definition
                           `(,name (&rest ,rest)
                                   (declare (dynamic-extent ,rest))
                                   ;; Function bodies for LABELS/FLET
                                   ;; are defined as lambdas.
                                   (apply
                                    ,(generate-wrapper-lambda-body
                                      lambda-list the-body :name name)
                                    ,rest)))))
                   definitions)
          ,@body))))
  (define-function-definer labels cl:labels)
  (define-function-definer flet cl:flet))

(cl:defun maybe-coerce-to-function (obj)
  (typecase obj
    (function-wrapper (function-object obj))
    (t obj)))

(cl:defun funcall (function &rest arguments)
  (cl:apply (maybe-coerce-to-function function) arguments))

(cl:defun apply (function arg &rest arguments)
  (cl:apply #'cl:apply (maybe-coerce-to-function function)
            arg arguments))

(defmacro multiple-value-call (function &rest forms)
  `(cl:multiple-value-call (maybe-coerce-to-function ,function)
     ,@forms))

Name: Just Testing, Ignore. 2010-04-22 5:41

;;; examples.lisp:

;;; Examples:

(various-utils:meta
  `(cl:defpackage #:function-arguments-user
     (:nicknames #:fun-args-user)
     (:shadowing-import-from #:fun-args ,@fun-args:+shadowed-exports+)
     (:use #:cl #:fun-args)))

(in-package #:fun-args-user)

(funcall
 (lambda (n)
   (unless (zerop n)   
     (cons n
           (funcall this-function
                    (1- (first (arguments this-function)))))))
 10) ;=> (10 9 8 7 6 5 4 3 2 1)

(defun test-fn1 ()
  (test-fn2 3 4)) ;=> TEST-FN1

(defun test-fn2 (arg1 arg2)
  (list (function-name this-function)
        (function-lambda-list this-function)
        (arguments this-function)
        (function-object this-function)
        (caller this-function)
        this-function
        arg1 arg2))

(test-fn2 1 2) ;=> (TEST-FN2 (ARG1 ARG2) (1 2) #<FUNCTION TEST-FN2> NIL
;                   #<FUNCTION-WRAPPER #<FUNCTION TEST-FN2> {2486E391}> 1 2)
(test-fn1) ;=> (TEST-FN2 (ARG1 ARG2) (3 4) #<FUNCTION TEST-FN2>
;               #<FUNCTION-WRAPPER #<FUNCTION TEST-FN1> {24840C01}>
;               #<FUNCTION-WRAPPER #<FUNCTION TEST-FN2> {24840C49}> 3 4)

(funcall
 (lambda ()
   (flet ((a-function (a b c) (values *call-stack* (+ a b c))))
     (a-function 1 2 3))))
;=> (#<FUNCTION-WRAPPER #<CLOSURE # {24939E85}> {24939EA9}>
;    #<FUNCTION-WRAPPER #<CLOSURE # {24939E2D}> {24939E41}>),6

(let (*print-function-object-long-form*)
  (flet ((a-function (a b c) (list (caller this-function)
                                   *call-stack* (+ a b c))))
    (print (a-function 1 2 3))
    (setq *print-function-object-long-form* t)
    (print (a-function 1 2 3))
    (print (funcall (lambda () this-function)))))       
;=> prints
;(NIL (#<FUNCTION-WRAPPER A-FUNCTION {24795909}>) 6)
;(NIL (#<FUNCTION-WRAPPER # {247962B9}>) 6)
;#<FUNCTION-WRAPPER #<CLOSURE (COMMON-LISP:LAMBDA (&REST #:REST11)) {24796C8D}> {24796C99}>

(labels ((test-local1 ()
           (test-local2 3))
         (test-local2 (n)
           (declare (ignorable n)) ; declarations work!
           (list *call-stack*
                 this-function)))
  (test-local1))
;=> ((#<FUNCTION-WRAPPER # {24B79D91}> #<FUNCTION-WRAPPER # {24B79D39}>)
;    #<FUNCTION-WRAPPER #<CLOSURE # {24B79D7D}> {24B79D91}>)

; this-function on inspection:
;#<FUNCTION-WRAPPER {24B79D91}>
;--------------------
;Class: #<STANDARD-CLASS FUNCTION-WRAPPER>
;--------------------
;
;[ ]  NAME        = TEST-LOCAL2
;[ ]  LAMBDA-LIST = (N)
;[ ]  ARGUMENTS   = (3)
;[ ]  OBJECT      = #<CLOSURE (COMMON-LISP:LAMBDA (&REST #:REST956)) {24B79D7D}>
;[ ]  CALLER      = #<FUNCTION-WRAPPER #<CLOSURE (COMMON-LISP:LAMBDA (&REST #:REST953)) {24B79D..
;
; (caller this-function) :
;#<FUNCTION-WRAPPER {24B79D39}>
;--------------------
;Class: #<STANDARD-CLASS FUNCTION-WRAPPER>
;--------------------
;
;[ ]  NAME        = TEST-LOCAL1
;[ ]  LAMBDA-LIST = NIL
;[ ]  ARGUMENTS   = NIL
;[ ]  OBJECT      = #<CLOSURE (COMMON-LISP:LAMBDA (&REST #:REST953)) {24B79D2D}>
;[ ]  CALLER      = NIL

;;; OP's example translated to Common Lisp and using this library:

(defun weird-fibs (n)
  (funcall
   (lambda (i)
     (if (= i 1) 0
         (+ (funcall this-function (1- i))
            (funcall (lambda (i)
                       (if (= i 1) 1
                           (funcall (caller this-function)
                                    (1- i))))
             (1- i)))))
   (1+ n)))

(loop for i from 0 to 10 collect (weird-fibs i))
;=> (0 1 1 2 3 5 8 13 21 34 55))

Name: Anonymous 2011-02-03 5:05

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