What is Expresiveness in a Computer Language?

X

Xah Lee

What is Expresiveness in a Computer Language

20050207, Xah Lee.

In languages human or computer, there's a notion of expressiveness.

English for example, is very expressive in manifestation, witness all
the poetry and implications and allusions and connotations and
dictions. There are a myriad ways to say one thing, fuzzy and warm and
all. But when we look at what things it can say, its power of
expression with respect to meaning, or its efficiency or precision, we
find natural languages incapable.

These can be seen thru several means. A sure way is thru logic,
linguistics, and or what's called Philosophy of Languages. One can also
glean directly the incapacity and inadequacy of natural languages by
studying the artificial language lojban, where one realizes, not only
are natural languages incapable in precision and lacking in efficiency,
but simply a huge number of things are near impossible to express thru
them.

One thing commonly misunderstood in computing industry is the notion of
expressiveness. If a language has a vocabulary of (smile, laugh, grin,
giggle, chuckle, guffaw, cackle), then that language will not be as
expressive, as a language with just (severe, slight, laugh, cry). The
former is "expressive" in terms of nuance, where the latter is
expressive with respect to meaning.

Similarly, in computer languages, expressiveness is significant with
respect to semantics, not syntactical variation.

These two contrasting ideas can be easily seen thru Perl versus Python
languages, and as one specific example of their text pattern matching
capabilities.

Perl is a language of syntactical variegations. Python on the other
hand, does not even allow changes in code's indentation, but its
efficiency and power in expression, with respect to semantics,
showcases Perl's poverty in specification.

http://xahlee.org/perl-python/what_is_expresiveness.html

© Copyright 2005 by Xah Lee.

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
P

Philipp H. Mohr

Hello,
I got a newbie question, I have written the following distance function:

def distance(self,element1, element2):
dist = 0

for n in range(len(element1)):
dist = dist + pow((element1[n] - element2[n]),2)
print 'dist' + dist
return sqrt(dist)


and in order to be able to use len() and index element1[] the function
needs to know that the arguments element1 and element2 are both listst or
doesn't it ?

I get the following error msg:

Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 75, in distance
for n in range(len(element1)):
TypeError: len() of unsized object



AND if I take len out I get:



Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 76, in distance
dist = dist + pow((element1[n] - element2[n]),2)
TypeError: unsubscriptable object



Thank you very much for your help.

Phil
 
J

Jorgen Grahn

(You posted your question as a followup to oen of Xah Lee's musings.
That is not the best of ideas, since people with threaded newsreaders
tend not to see it. Just post (creating a new thread) next time.)

Hello,
I got a newbie question, I have written the following distance function:

def distance(self,element1, element2):
dist = 0

for n in range(len(element1)):
dist = dist + pow((element1[n] - element2[n]),2)
print 'dist' + dist
return sqrt(dist)

and in order to be able to use len() and index element1[] the function
needs to know that the arguments element1 and element2 are both listst or
doesn't it ?

Yeah, or something else supporting len(x) and x[n].
I get the following error msg: ....
TypeError: len() of unsized object ....
AND if I take len out I get: ....
TypeError: unsubscriptable object

Well, that depends on the types of the arguments
you're passing to the method, doesn't it? A call like

distance([1,1], [0,0])

works fine for me. And here is a slightly simpler implementation
which avoids len (thus accepting a wider range of argument types)
and pow:

def distance(element1, element2):
dist=0
for a, b in zip(element1, element2):
n = float(a-b)
dist += n*n
return math.sqrt(dist)

(Dunno if that's the correct formula for n-dimensional distance.
I have forgotten way too much math in the past ten years.)

/Jorgen
 
G

George Sakkis

Philipp H. Mohr said:
Hello,
I got a newbie question, I have written the following distance function:

def distance(self,element1, element2):
dist = 0

for n in range(len(element1)):
dist = dist + pow((element1[n] - element2[n]),2)
print 'dist' + dist
return sqrt(dist)


and in order to be able to use len() and index element1[] the function
needs to know that the arguments element1 and element2 are both listst or
doesn't it ?

No it doesn't; it finds out at runtime. Also they don't have to be
list; any object that defines __len__ will do.
I get the following error msg:

Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 75, in distance
for n in range(len(element1)):
TypeError: len() of unsized object

So obviously n.getCenter is not a list.
AND if I take len out I get:



Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 76, in distance
dist = dist + pow((element1[n] - element2[n]),2)
TypeError: unsubscriptable object

Same problem; n.getCenter and/or newElement are not lists (or other
subscriptable objects for that matter). Just print them before you call
distance to convince yourself.

By the way, you don't use 'self' anywhere, so perhaps distance should
be a function, not method. Also, the pythonic way of writing it (since
2.4) is:

from math import sqrt
from itertools import izip

def distance(sequence1, sequence2):
return sqrt(sum((x-y)**2 for x,y in izip(sequence1, sequence2)))
Thank you very much for your help.

Phil

Regards,
George
 
R

raptor

I think u are wrong.. I think perl is much more exrpressive in
semantics than Python..
 
S

Steven D'Aprano

I think u are wrong.. I think perl is much more exrpressive in
semantics than Python..

Well, with such great command of natural language as you are displaying,
how could anyone argue with your conclusion?
 
P

Paul McGuire

Is getCenter a function? If so, you need to invoke distance using:
dist = self.distance( n.getCenter(), newElement )

Of course, that is assuming that newElement is a local variable of type
list/tuple/etc.

-- Paul
 
S

Steven D'Aprano

Hello,
I got a newbie question, I have written the following distance function:

Great. Now, how about you tell us what you expect it to do? I assume
it calculates the Euclidean distance between two points. (If you
don't know what Euclidean distance is, don't worry about it -- it is
basically just the "normal" distance.)

Should it calculate the distance between points on a line? 2-D points on
a plane? 3-D points in space? Higher dimensions?

I'm going to assume you want your function to operate on any number of
dimensions.
def distance(self,element1, element2):

You don't need "self" unless it is a method of a class.
dist = 0
for n in range(len(element1)):
dist = dist + pow((element1[n] - element2[n]),2)
print 'dist' + dist

Why are you printing the SQUARE of the distance?
return sqrt(dist)

Okay, here is a simpler method, complete with comments:

def distance(point1, point2):
"""Return the distance between two points in N-dimensional space.

Example: if the points are (a,b) and (c,d) respectively, returns the
Euclidean distance d = sqrt( (c-a)**2 + (d-b)**2 )

Both points should have the same number of coordinates, each of which
is a float or int. Points can be given as lists or tuples.
"""
sum_sq = 0 # sum of squares of the differences
if len(point1) != len(point2):
# check for errors here because they might not be picked
# up later if we don't
raise ValueError("Points have different number of dimensions")
for i in range(len(point1)):
x, y = point1, point2 # extract the next two ordinates
sum_sq = sum_sq + (y-x)**2 # and add the square of the difference
return sum_sq**0.5

If you really care about accuracy, you should use the sqrt function from
the math module rather than **0.5.

Here is another version, using zip.

def distance(point1, point2):
if len(point1) != len(point2):
raise ValueError("Points have different number of dimensions")
L = zip(point1, point2)
sum_sq = 0
for x,y in L:
sum_sq = sum_sq + (y-x)**2
return sum_sq**0.5

And finally, a one-liner using list comprehensions and no error-checking.

def distance(point1, point2):
return sum([(y-x)**2 for x,y in zip(point1, point2)])**0.5


and in order to be able to use len() and index element1[] the function
needs to know that the arguments element1 and element2 are both listst
or doesn't it ?

No. Lots of things can be passed to len() and indexed.
I get the following error msg:

Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 75, in distance
for n in range(len(element1)):
TypeError: len() of unsized object

And what is element1 when you get that error message? That's the first
thing I would look at.
AND if I take len out I get:


Traceback (most recent call last):
File "Memory.py", line 105, in ?
start.inputVector(inP2)
File "Memory.py", line 97, in inputVector
allDimensions[0].newAttribute(vector)
File "Memory.py", line 56, in newAttribute
dist = self.distance(n.getCenter,newElement)
File "Memory.py", line 76, in distance
dist = dist + pow((element1[n] - element2[n]),2)
TypeError: unsubscriptable object

Firstly, you seem to have a lot of modules there. What happens when you
call distance directly? Can you get it to work then?

Secondly, what are the values for element1 and element2?
 
P

Philipp H. Mohr

Hello,

thank you very much for all your help. I have solved the problem - you
guys where right, the problem was some where else.

I have another class which got an accessor:

def getCenter(self):
global center
return center


and I called it by saying n.getCenter, but this returns:

<bound method ARBgps.getCenter of <__main__.ARBgps instance at
0xb7c368ec>>

I changed it to n.getCenter() and now it works. Despite some small
problems I am really getting to like python.

Thank you,
Phil
 
P

Pete Barrett

Similarly, in computer languages, expressiveness is significant with
respect to semantics, not syntactical variation.
It may just be me, but I tend to think of a computer language as a
tool for directing computers to perform specific actions. Do we talk
about the expressiveness of a spade?

There's a similar concept in the 'possible uses' of a tool (a spade is
an excellent tool for digging the garden, but you wouldn't use it to
clean your teeth; you *could* use a toothbrush to dig the garden, but
you wouldn't if a spade was available). Similarly with computer
languages - some are better for certain tasks than others, but I don't
think 'expressiveness' is the way to describe that.

Pete Barrett
 
J

Jürgen Exner

Xah said:
What is Expresiveness in a Computer Language[...]

Just for the records at Google et.al. in case someone stumbles across Xah's
masterpieces in the future:
Xah is very well known as the resident troll in many NGs and his
'contributions' are less then useless.

Best is to just ignore him.

But for heaven's sake unless you want to embarrass yourself really badly
don't take any of his postings serious because he has proven again and again
that he has no clue whatsoever about computer science or programming.

jue
 
J

James Dennett

Pete said:
It may just be me, but I tend to think of a computer language as a
tool for directing computers to perform specific actions. Do we talk
about the expressiveness of a spade?

Spades, however, are rarely used for communication between
human beings, whereas languages, definitely including
programming languages, are. While there's a good case to
be made that communication from a person to a computer is
the *primary* use of a programming language, communication
between programmers is a major secondary use.
There's a similar concept in the 'possible uses' of a tool (a spade is
an excellent tool for digging the garden, but you wouldn't use it to
clean your teeth; you *could* use a toothbrush to dig the garden, but
you wouldn't if a spade was available). Similarly with computer
languages - some are better for certain tasks than others, but I don't
think 'expressiveness' is the way to describe that.

You're missing, maybe, the very special nature of one task,
namely that of communication between programmers. Computers
care not if you use assembly code or Python, but people often
find one or the other easier to read and understand. (Which
is considered easier depends on the background of the reader.)

-- James
 
K

Keith Thompson

Xah Lee said:
What is Expresiveness in a Computer Language

20050207, Xah Lee.

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT | :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================
 
D

Dennis Lee Bieber

def getCenter(self):
global center
return center
That's rather futile-looking...

You've made a method which does absolutely nothing with the
instance, but return some global data value -- and the "global"
statement isn't even needed as Python will read globals without it; it
is only needed if an assignment to the name is required to be global.
and I called it by saying n.getCenter, but this returns:
You can replace "n.getCenter()" with just "center" to access the
global directly.


Perhaps you mean to maintain a "center" for EACH instance, but
in that case you need to remove the "global" and return "self.center"
(with similar changes to anything that computes a new value of center).

--
 
K

Kenny McCormack

It may just be me, but I tend to think of a computer language as a
tool for directing computers to perform specific actions. Do we talk
about the expressiveness of a spade?

Most people think of natural language as "a tool for directing people to
perform specific actions" and have as much need for anything beyond that as
you seem to have.

Luckily, some people (writers, artists) are able to do more with natural
language than boss people around. But make no mistake, that is its primary
purpose.
 
M

Matthias Buelow

Steven D'Aprano said:
Well, with such great command of natural language as you are displaying,
how could anyone argue with your conclusion?

Folks, Xah Lee is a known troll.. don't get into any arguments among
yourselves over anything that person writes.

mkb.
 
C

Casey Hawthorne

It is easier to write code a computer can understand than others can
understand!

It is harder to read code than to write code!
 
R

Ramon F Herrera

[Peter Barret wrote:]
It may just be me, but I tend to think of a computer language as a
tool for directing computers to perform specific actions. Do we talk
about the expressiveness of a spade?

yes, it is just you. :)

Your comparison is a very poor match. How can you even begin to
compare a hammer or a screwdriver with a computer language?

A more apt comparison would be a Roman general with his spade directing
his troops to attack. Some generals have more expresiveness than
others. The barbar generals will just grunt while the French generals
will inspire with some poetic invocation.

-Ramon
 
R

Randy Howard

Keith Thompson wrote
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================

Out of curiosity, does anyone remember who 'jgs' refers to
above?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top