Re: A style question



job-271842874@xxxxxxxxxxxxxx schrieb:

Another friend of mine commenting on the same FizzBuzz thread supplied the following Python code. It certainly is concise:

for i in xrange(1,101):
print(str(i), "Fizz", "Buzz", "FizzBuzz")[(i%3==0)|(i%5==0)<<1]

Forgive my ignorance, but is anything like the boolean bit shifting technique used in the Python code above possible in Lisp? No big loss if it isn't, just curious.

In principle you could do exactly that - if you also provide this
strange shifting operator:

>>> (11%3==0)|(11%5==0)
False
>>> (12%3==0)|(12%5==0)
True
>>> (20%3==0)|(20%5==0)
True
>>> (30%3==0)|(30%5==0)
True


And:

>>> False<<1
0
>>> True<<1
2

Okay, so we expect << to throw out 0, 2, 2 and 2 for the three cases.
But:

>>> (11%3==0)|(11%5==0)<<1
0
>>> (12%3==0)|(12%5==0)<<1
1
>>> (20%3==0)|(20%5==0)<<1
2
>>> (30%3==0)|(30%5==0)<<1
3

It seems truth values would be structs in Lisp. They not only have a
boolean value of t or nil, but they also represent some number.

Anyway, let's say you provide <<, then you could say:

(loop for i upto 100 do
(print (nth (<< (mod i 3) (mod i 5)) (list i "Fizz" "Buzz" "FizzBuzz")))

This version has now a smaller complexity with its 20 tokens as the
Python version with its 28 tokens.
Okay, if you count the definition of << in Lisp, then Python wins here,
because it already is defined there.


I suppose it's unreasonable to expect the Lisp version to be as concise as the Python version

No, it is reasonable. And as you see: Python is complexity wise 40% worse.


> - not only is this a toy example, but I think a
language with more syntax will be able to provide more brevity in certain situations.

You are right. For people who are happy to write scripts Python will
be an excellent choice. Scripting is the domain of Python and usually
it will do very well with 10 line programs.
But if we are talking about something that goes beyond 3k LOC things
change. Then Lisp will have a more specialized syntax and win.
So it is the question of complexity. For easy problems Lisp will most
likely not be much better than Python - but probably also not worse.
But as complexity grows you are better off with Lisp.



That's a tradeoff I'm willing to accept given the benefits of a syntax that's more readily parsed and manipulated.

Clever choice.


André
--
.



Relevant Pages

  • Re: Python syntax in Lisp and Scheme
    ... Consider this python code: ... 15 while stack: ... 12;; like os.listdir, but traverses directory trees ... Forgetting to indent properly in a lisp program does not yield ...
    (comp.lang.python)
  • Re: Python syntax in Lisp and Scheme
    ... Consider this python code: ... 15 while stack: ... 12;; like os.listdir, but traverses directory trees ... Forgetting to indent properly in a lisp program does not yield ...
    (comp.lang.lisp)
  • Re: Python syntax in Lisp and Scheme
    ... > the parens until the matching one at the beginning of the DOLIST begins ... > Forgetting to indent properly in a lisp program does not yield ... of course forgetting to indent does not *directly* yield erroneous code. ... if you make an edit in python the result of this edit is immediately ...
    (comp.lang.python)
  • Re: Python syntax in Lisp and Scheme
    ... > the parens until the matching one at the beginning of the DOLIST begins ... > Forgetting to indent properly in a lisp program does not yield ... of course forgetting to indent does not *directly* yield erroneous code. ... if you make an edit in python the result of this edit is immediately ...
    (comp.lang.lisp)
  • Re: which lisp book ?
    ... what's so common between python and lisp? ... coming from a Python background. ... They're both "practical" languages which, ... syntax differences. ...
    (comp.lang.lisp)