Re: A style question





Ken Tilton wrote:


Rob Warnock wrote:

Ken Tilton <kentilton@xxxxxxxxx> wrote:
+---------------
| ps. Me, I am not starting until we get the functional requirements | cleared up. k
+---------------

See my reply to <nallen05@xxxxxxxxx>'s MOD-free code-generating macro,
or take this instead:

Macro DEF-FIZZ-BUZZ -- Define a "fizz-buzz"-generating function

Syntax:
def-fizz-buzz fname alist ==> fname

Arguments and Values:
fname -- A symbol, the name of the function to be defined (as by DEFUN).

alist -- an association list, whose keys are prime integers > 1
and whose values are strings.

Description:

DEF-FIZZ-BUZZ defines a function named FNAME of one argument,


er, two?

a positive integer. When called with an argument (say) N, FNAME
will print each positive integer starting with 1


I am concerned that we are misunderstanding each other, since 1 would never be divisible by any prime integer > 1.

... below N,


Did you mean "to N"? I ask only because that was the original spec and it sounded inclusive and I have not seen anything necessitating a change. Just want to make sure that was intended.

followed
by a newline, except that if any of the keys of the ALIST evenly
divide the current integer, then the corresponding string value(s)
of the key(s) dividing the current integer will be printed instead
of the integer itself. Note: If multiple keys divide the current
integer, all of the corresponding string values will be printed,
in the same order as the elements of the ALIST. Only one copy
of any string value will be printed for any current integer.

Examples:

(def-fizz-buzz 'fizz-buzz '((3 . "Fizz") (5 . "Buzz"))) ==> FIZZ-BUZZ

(fizz-buzz 22)
>> 1
>> 2
>> Fizz
>> 4
>> Buzz
>> Fizz
>> 7
>> 8
>> Fizz
>> Buzz
>> 11
>> Fizz
>> 13
>> 14
>> FizzBuzz
>> 16
>> 17
>> Fizz
>> 19
>> Buzz
>> Fizz
>> 22
==> NIL

(def-fizz-buzz 'very-fizzy
'((2 . "Burp") (3 . "Fizz") (5 . "Buzz") (7 . "Bang")))
==> VERY-FIZZY

(very-fizzy 16)
>> 1
>> Burp
>> Fizz
>> Burp
>> Buzz
>> BurpFizz
>> Bang
>> Burp
>> Fizz
>> BurpBuzz
>> 11
>> BurpFizz
>> 13
>> BurpBang
>> FizzBuzz
>> Burp
==> NIL

There's your spec. Where's your code? ;-}


Hunh? Right here:

"...print each positive integer starting with 1 below N, followed
by a newline, except that if any of the keys of the ALIST evenly
divide the current integer, then the corresponding string value(s)
of the key(s) dividing the current integer will be printed instead
of the integer itself. If multiple keys divide the current
integer, all of the corresponding string values will be printed,
in the same order as the elements of the ALIST. Only one copy
of any string value will be printed for any current integer."

I think most commercial Lisp compilers would accept that. if you are stuck with a "free" Lisp, try:

(defun fbuzz (n subs)
(loop for i below n

oops, "from 1"

And here is the less functional version:

(defun fbuzz2 (n subs)
(loop for i from 1 below n
do (loop with printed
for (d . sub) in subs
when (zerop (mod i d))
do (setf printed t)
(princ sub)
finally (unless printed (princ i))
(terpri))))

I just hate SETF. Which reminds me:

(defmd xlater ()
xlates
(xlation (c? (bwhen (v (^value))
(loop for (d . sub) in (^xlates)
when (zerop (mod v d))
collect sub))))
(view (c? (bif (xl (^xlation))
(apply 'concatenate 'string xl)
(^value)))))

(defobserver view ((self xlater))
(when new-value (print new-value)))

Test:

(loop with xl = (make-instance 'xlater
:value (c-in nil)
:xlates '((3 . "Fizz")(5 . "Buzz")))
for n from 1 below 20
do (setf (value xl) n))

Damn setf.

kt

--
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
-- Elwood P. Dowd

In this world, you must be oh so smart or oh so pleasant.
-- Elwood's Mom
.



Relevant Pages

  • Re: A style question
    ... then the corresponding string value ... If multiple keys divide the current ... (defun fbuzz (n subs) ...
    (comp.lang.lisp)
  • Re: Help requested to finish Array code
    ... Dim OrigStr As String ... Dim MainS As String ... Dim SubS As String ...
    (microsoft.public.excel.programming)
  • Re: play sound when opening workbook
    ... Gives everything kind of a C/C++ feeling where subs just don't ... And heck this way you can access it from a spreadsheet and make ... > The reason is because I adapted a piece of code in my library. ... >>> Public Function PlayWavFile(WavFile As String) As String ...
    (microsoft.public.excel.programming)
  • Re: Where to place Dim Satements
    ... I'm not sure how he response stuff would fit in--if you want to quit, ... Private Function GetFileList(Pattern As String, ... Dim f As String ... > subs, I find sometimes that the value in a variable doesn't work any more. ...
    (microsoft.public.excel.misc)
  • Re: Multiple Events
    ... Function MakeWholeName(strLast As String, strFirst As String) As String ... Functions and subs can be called from a Function or Sub ... >> You can have a BeforeUpdate event for each control on a form, ...
    (microsoft.public.access.modulesdaovba)