|
|
|
|
|
by mg
200 days ago
|
|
The first example in the lanuage introduction (https://homepages.cwi.nl/~steven/abc/): HOW TO RETURN words document:
PUT {} IN collection
FOR line IN document:
FOR word IN split line:
IF word not.in collection:
INSERT word IN collection
RETURN collection
In Python it would be: def words(document):
collection = set()
for line in document:
for word in line.split():
if word not in collection:
collection.add(word)
return collection
I kept the splitting by line and "if word not in collection:" in there even though they don't have an impact on the outcome. I have the feeling that even in the original example they have only been put there to show the language constructs, not to do anything useful. If one wanted to optimize it, it could all be collapsed to just "return set(text.split())", but that would not show off the language features.ABC uses 225 chars, Python 218 chars. 3% less. So one could say Python is 3% more efficient than ABC. |
|