beginning with ML
- From: "michele.simionato@xxxxxxxxx" <michele.simionato@xxxxxxxxx>
- Date: Mon, 05 Nov 2007 05:36:33 -0000
Yesterday, while performing an Internet search for lightweight
concurrency,
I run into the Alice system, which is an implementation of the ML
language.
I knew nothing about ML, except that a functional language called ML
existed,
however skimming the tutorial I pretty much liked what I saw, so I
want to do
some experiment with ML programming, at least for educational
purposes.
If you have good pointers to resources, references, libraries, etc,
please let me know. I am especially interested in code snippets and
example of idiomatic ML (an ML cookbook or something like that). I
have already discovered that there is a "SICP in ML" resource, which I
am looking at. My first impression if that ML has been heavily
influenced
by Scheme (it looks like it has been designed as a *reaction* to
Scheme)
which I am familiar with, so a reference like "ML for Scheme
programmers"
would be fine for me. Anyway, I do most of my work in Python, so I am
especially interested in understanding how to convert Python idioms in
ML
idioms. To be concrete, I have made up a problem (which is however not
completely made up, since I had to parse files in custom formats many
times in
real life). Suppose I have a data file like the following:
$ cat example.dat
x
1.0
2.0
3.0
y
-0.5
0.5
z
2
3.0
I want to write a script taking this file as input and returning the
averages as output:
Averages:
x = 2.0
y = 0.0
z = 2.5
The script must recognize the identifiers and the numbers (possibly
converting
integers in floats) and give some error message for wrong inputs. An
idiomatic
modern Python solution could be the following:
import re
def show_averages(names_averages):
print 'Averages:'
for name, average in names_averages:
print '%s = %s' % (name, average)
def apply_(consumer, producer):
for line in producer:
consumer.send(line)
return consumer.send('*END*')
def is_valid_identifier(name):
return re.match('[_a-zA-Z][_\w\d]*', name)
def is_number(line):
try:
float(line)
except TypeError:
return False
else:
return True
def make_parser():
names = [] # valid identifiers
sums = {} # identifiers -> sums
counts = {} # identifiers -> counts
current_name = None
while True:
line = yield
if line == '*END*':
# possibly add a check for counts[n] != 0
yield [(n, sums[n]/counts[n]) for n in names]; return
elif is_valid_identifier(line):
current_name = line
sums[current_name] = 0
counts[current_name] = 0
names.append(current_name)
elif is_number(line):
assert current_name, 'No valid identifier in the input
data!'
sums[current_name] += float(line)
counts[current_name] += 1
else:
raise ValueError('Bad line %r' % line)
def test():
data = '''
x
1.0
2.0
3.0
y
-0.5
0.5
z
2
3.0
'''.split()
parser = make_parser()
parser.next() # initialize
show_averages(apply_(parser, data))
if __name__ =='__main__':
test()
How would you write an idiomatic ML solution?
Thanks in advance to everybody,
Michele Simionato
P.S. I was not sure if to post here or in comp.lang.ml, but it looks
like
there is very little traffic on comp.lang.ml. Apologies if this the
wrong
newsgroup.
.
- Follow-Ups:
- Re: beginning with ML
- From: Jon Harrop
- Re: beginning with ML
- From: Chris Rathman
- Re: beginning with ML
- From: Vesa Karvonen
- Re: beginning with ML
- Prev by Date: Fwd: Re: Growth of the CAML family of languages
- Next by Date: ML functors vs. C++ templates
- Previous by thread: ANNOUNCE: GHC version 6.8.1
- Next by thread: Re: beginning with ML
- Index(es):
Relevant Pages
|