all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* some more material on the Lisp data/code psychedelia
@ 2014-01-04 19:37 Emanuel Berg
  2014-01-04 20:13 ` Thien-Thi Nguyen
       [not found] ` <mailman.11016.1388866201.10748.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 5+ messages in thread
From: Emanuel Berg @ 2014-01-04 19:37 UTC (permalink / raw)
  To: help-gnu-emacs

The whole discussion on types and the Lisp data/code
blend was the source of some confusion. But perhaps
that's nothing to be ashamed of.

I thought it would be clearer if I could turn it into
actual code. So if you don't mind, please comment on
this.

(defun increment-last (l)
  (interactive)
  (if (not l) '()
    (let ((hd (car l))
          (tl (cdr l)) )
      (if (not tl)
          (list (+ 1 hd))
        (cons hd (increment-last tl) )))))

;; test
(increment-last '())       ; => '()
(increment-last '(1))      ; => '(2)
(increment-last '(1 2 3))  ; => '(1 2 4)

(defun do-change-me ()
  (interactive)
  (fset 'do-change-me
        (increment-last (symbol-function 'do-change-me)) )
  1)

(do-change-me) ; hit `C-x e' repeatedly here

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: some more material on the Lisp data/code psychedelia
  2014-01-04 19:37 some more material on the Lisp data/code psychedelia Emanuel Berg
@ 2014-01-04 20:13 ` Thien-Thi Nguyen
       [not found] ` <mailman.11016.1388866201.10748.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 5+ messages in thread
From: Thien-Thi Nguyen @ 2014-01-04 20:13 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 840 bytes --]

() Emanuel Berg <embe8573@student.uu.se>
() Sat, 04 Jan 2014 20:37:15 +0100

   (defun increment-last (l)
     (interactive)
     (if (not l) '()
       (let ((hd (car l))
             (tl (cdr l)) )
         (if (not tl)
             (list (+ 1 hd))
           (cons hd (increment-last tl) )))))

You can also use ‘last’ and ‘setcar’ (via ‘incf’) for this:

 (defun increment-last (ls) 
   (when ls 
     (incf (car (last ls)))
     ls))

Non-recursive means less stack required for long forms.  Destructive
style is sketchy but the func is already squarely in DWR territory...

-- 
Thien-Thi Nguyen
   GPG key: 4C807502
   (if you're human and you know it)
      read my lisp: (responsep (questions 'technical)
                               (not (via 'mailing-list)))
                     => nil

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: some more material on the Lisp data/code psychedelia
       [not found] ` <mailman.11016.1388866201.10748.help-gnu-emacs@gnu.org>
@ 2014-01-04 20:24   ` Emanuel Berg
  2014-01-04 23:40     ` Emanuel Berg
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2014-01-04 20:24 UTC (permalink / raw)
  To: help-gnu-emacs

Thien-Thi Nguyen <ttn@gnu.org> writes:

> You can also use ‘last’ and ‘setcar’ (via ‘incf’) for
> this:
>
>  (defun increment-last (ls) (when ls (incf (car (last
> ls))) ls))
>
> Non-recursive means less stack required for long
> forms.  Destructive style is sketchy but the func is
> already squarely in DWR territory...

That's interesting:

(setq *ls* '(1 2 3))

(defun inc-last (ls)
  (when ls
    (incf (car (last ls)))
    ls) )

(inc-last *ls*)
*ls* ; => '(1 2 4) after the above line

I wonder how the "destructiveness" can be used with
respect to the rest of the demo...?

DWR = Design with Reuse?

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: some more material on the Lisp data/code psychedelia
  2014-01-04 20:24   ` Emanuel Berg
@ 2014-01-04 23:40     ` Emanuel Berg
  2014-01-17  1:29       ` Emanuel Berg
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2014-01-04 23:40 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

>> You can also use ‘last’ and ‘setcar’ (via ‘incf’)
>> for this: (defun increment-last (ls) (when ls (incf
>> (car (last ls))) ls)) Non-recursive means less stack
>> required for long forms.  Destructive style is
>> sketchy but the func is already squarely in DWR
>> territory...
>
> That's interesting:
>
> (setq *ls* '(1 2 3))
>
> (defun inc-last (ls) (when ls (incf (car (last ls)))
> ls) )
>
> (inc-last *ls*) *ls* ; => '(1 2 4) after the above line
>
> I wonder how the "destructiveness" can be used with
> respect to the rest of the demo...?

How about:

(defun change-me ()
  (incf (car (last (symbol-function 'change-me))))
  1)

(change-me) ; hit `C-x e' repeatedly here

Ironic thing is, first version, while not as good,
actually showed the duality in a cool way. The above
version just looks stiff, its purpose unclear.

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: some more material on the Lisp data/code psychedelia
  2014-01-04 23:40     ` Emanuel Berg
@ 2014-01-17  1:29       ` Emanuel Berg
  0 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2014-01-17  1:29 UTC (permalink / raw)
  To: help-gnu-emacs

;; Another classic demo for you all to vibe with.
;;
;; Sort of OO data/method encapsulation, and sort of
;; XML unrestricted markup, to do run-time data/type
;; consistency.
;;
;; (HELP! I'm a Lisp addict!)

;; version one
(setq *typed-symbol* '(0 integerp))

;; version two (combination of types)
(setq *typed-symbol* '(0.0 (lambda (val)
                             (or (floatp val)
                                 (integerp val) ))))
(fset
 '*typed-symbol*
 '(lambda (&optional new-data)
    (progn
      (if new-data
          (if (eval `(,(cadr *typed-symbol*) new-data))
              (setcar *typed-symbol* new-data)
            (error "Wrong type") ))
      (car *typed-symbol*) )))

(*typed-symbol*)     ; 0 (or 0.0), no change
(*typed-symbol* 4)   ; 4, cool
(*typed-symbol* 4.0) ; doesn't work for version one
(*typed-symbol* "disrespecting the type, man") ; error
(*typed-symbol*)     ; still 4 (or 4.0)

;; or, if desire to use a (half) normal variable:

(setq *integer-symbol* 1)

(fset
 '*integer-symbol*
 '(lambda (new-data)
    (if (integerp new-data)
        (setq *integer-symbol* new-data)
      (error "Wrong type: Integers only") )))

*integer-symbol*       ; 1
(*integer-symbol* 2)   ; 2
*integer-symbol*       ; 2
(*integer-symbol* 2.0) ; no floats
(*integer-symbol* "no strings") ; error

-- 
underground experts united:
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-01-17  1:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-04 19:37 some more material on the Lisp data/code psychedelia Emanuel Berg
2014-01-04 20:13 ` Thien-Thi Nguyen
     [not found] ` <mailman.11016.1388866201.10748.help-gnu-emacs@gnu.org>
2014-01-04 20:24   ` Emanuel Berg
2014-01-04 23:40     ` Emanuel Berg
2014-01-17  1:29       ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.