From: John Clonts
Subject: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <3A10A751.2B9B4856@mastnet.net>
I have taken it as a newbie assignment to write a studlify function
since my emacs didn't seem to have it built in (however this is written
in acl6).

I am asking for comments regarding technique and/or style.  It seems
that there should be a more concise approach than the string stream?

Thanks,
John

(studlify "Now is the time for all good men to come to the aid")
==>NoW is The tiMe fOR aLl gOOD MEn To come TO tHe aiD

(defun studlify (instr)
  "return a copy of instr with randomly toggled case"
  (let ((ss (make-string-output-stream)))
    (loop 
	for c across instr
	doing (princ (random-toggle-case c) ss))
    (get-output-stream-string ss)))

(defun random-toggle-case (c)
  (if (> 4 (random 9))			; toggle 4 out of 9
      (toggle-case c)
    c))

(defun toggle-case (c)
  (if (upper-case-p c) 
      (char-downcase c)
    (char-upcase c)))

From: Pierre R. Mai
Subject: Re: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <871ywe6a17.fsf@orion.bln.pmsf.de>
John Clonts <·······@mastnet.net> writes:

> I have taken it as a newbie assignment to write a studlify function
> since my emacs didn't seem to have it built in (however this is written
> in acl6).
> 
> I am asking for comments regarding technique and/or style.  It seems
> that there should be a more concise approach than the string stream?

[ studlify snipped, see comments by Kent on with-output-to-string ]

> (defun random-toggle-case (c)
>   (if (> 4 (random 9))			; toggle 4 out of 9
>       (toggle-case c)
>     c))
> 
> (defun toggle-case (c)
>   (if (upper-case-p c) 
>       (char-downcase c)
>     (char-upcase c)))

(defun studlify (string)
  "return a copy of string with randomly toggled case"
  (map 'string #'random-toggle-case string))

(defun nstudlify (string)
  "destructively and randomly toggle case of string"
  (map-into string #'random-toggle-case string))

There are of course lots of other ways to do it, but in this case I
think that mapping constructs are especially apropriate, since they
directly take advantage of the fact that input string and output
string will be identical in length and order, something that other
iteration constructs will have to do explicitly, e.g.

(defun studlify (string)
  (loop with result = (make-string (length string))
        for char across string
        for index from 0
        do
        (setf (char result index) (random-toggle-case char))
        finally (return result)))

Regs, Pierre.

P.S.: Another style suggestion:  I'd use longer variable names,
i.e. char or character instead of c, and string or input-string
instead of instr, etc.  This makes code and intent often much more
clearer.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: John Clonts
Subject: Re: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <3A11563A.643A@my-deja.com>
Pierre R. Mai wrote:
> 
> John Clonts <·······@mastnet.net> writes:
> 
> > I have taken it as a newbie assignment to write a studlify function
> > since my emacs didn't seem to have it built in (however this is written
> > in acl6).
> >
> > I am asking for comments regarding technique and/or style.  It seems
> > that there should be a more concise approach than the string stream?
> 
> [ studlify snipped, see comments by Kent on with-output-to-string ]
> 
> > (defun random-toggle-case (c)
> >   (if (> 4 (random 9))                        ; toggle 4 out of 9
> >       (toggle-case c)
> >     c))
> >
> > (defun toggle-case (c)
> >   (if (upper-case-p c)
> >       (char-downcase c)
> >     (char-upcase c)))
> 
> (defun studlify (string)
>   "return a copy of string with randomly toggled case"
>   (map 'string #'random-toggle-case string))
> 
> (defun nstudlify (string)
>   "destructively and randomly toggle case of string"
>   (map-into string #'random-toggle-case string))
> 
> There are of course lots of other ways to do it, but in this case I
> think that mapping constructs are especially apropriate, since they
> directly take advantage of the fact that input string and output
> string will be identical in length and order, something that other

Thanks!  That's certainly what I was looking for (I thought I had tried
it, but maybe I was trying to use mapcar).

Cheers,
John
From: Kent M Pitman
Subject: Re: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <sfwbsvjnndc.fsf@world.std.com>
John Clonts <·······@mastnet.net> writes:

> I have taken it as a newbie assignment to write a studlify function
> since my emacs didn't seem to have it built in (however this is written
> in acl6).
> 
> I am asking for comments regarding technique and/or style.  It seems
> that there should be a more concise approach than the string stream?

Well, first you should use WITH-OUTPUT-TO-STRING so you get stack allocation
of the relevant resources in implementations that set this up correctly.
MAKE-STRING-OUTPUT-STREAM doesn't provide the implementation enough advice
to be able to tell that you're not having the object used indefinitely and
so the GC will have to clean up after you.  Or if you're really worried
about speed, you should just copy the array (see copy-seq) and then iterate
over it setting each element with (setf (char ...) ...) or some such.

I don't know why concise is really relevant.  I've mostly commented on
efficiency. Using string output streams as you have is a very general hammer.
But the rest of your code looked ok (didn't try it, just glanced at it).

Side note:

Once a long time ago in Maclisp, we had a minor fight in my office (me
and JonL White mostly).  He kept naming all the internal functions of
Maclisp with weird names that were invariably |foo-internal/|| (where
"/" was the syntactic quoting character which is now "\").  I think
sometimes they were strangely cased, too, to avoid people typing them.
Irritated at all these strange names, which sometimes one wanted
access to and it was a pain to call, I finally made an emacs macro
(which I think JonL never used) which allowed you to type the name in
normal case "foo" and then would do the following:  take the name and add
up the ascii codes of its digits.  Use that number to initialize a random
number seed, so you'd get the same random series every time for a given word,
but a different random series for different words.  Then just use (random 2)
to decide whether to upcase or not.  Well, it was all written in Teco, but
same idea.  It would produce the appropriate |FoOBAr/|| by just typing
foobar without any risk you'd call the function by accident.
From: vsync
Subject: Re: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <86hf5ag8zt.fsf@piro.quadium.net>
Kent M Pitman <······@world.std.com> writes:

> to decide whether to upcase or not.  Well, it was all written in Teco, but
> same idea.  It would produce the appropriate |FoOBAr/|| by just typing
> foobar without any risk you'd call the function by accident.

This is downright hilarious.  You wouldn't by any chance happen to
have that code sitting around waiting to be posted to Usenet, would
you?  :)

-- 
vsync
http://quadium.net/ - last updated Mon Nov 13 01:46:23 PST 2000
(cons (cons (car (cons 'c 'r)) (cdr (cons 'a 'o))) ; Orjner
      (cons (cons (car (cons 'n 'c)) (cdr (cons nil 's))) nil))
From: Joe Marshall
Subject: Re: PLeAsE helP me sOlve tHis CaSE probLEM :)
Date: 
Message-ID: <7l66wdnq.fsf@content-integrity.com>
Kent M Pitman <······@world.std.com> writes:

> Once a long time ago in Maclisp, we had a minor fight in my office (me
> and JonL White mostly).  He kept naming all the internal functions of
> Maclisp with weird names that were invariably |foo-internal/|| (where
> "/" was the syntactic quoting character which is now "\").  I think
> sometimes they were strangely cased, too, to avoid people typing them.
> Irritated at all these strange names, which sometimes one wanted
> access to and it was a pain to call, I finally made an emacs macro
> (which I think JonL never used) which allowed you to type the name in
> normal case "foo" and then would do the following:  take the name and add
> up the ascii codes of its digits.  Use that number to initialize a random
> number seed, so you'd get the same random series every time for a given word,
> but a different random series for different words.  Then just use (random 2)
> to decide whether to upcase or not.  Well, it was all written in Teco, but
> same idea.  It would produce the appropriate |FoOBAr/|| by just typing
> foobar without any risk you'd call the function by accident.

Perhaps we need a new value of readtable-case,  :studly


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----