beginning with ML



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.

.



Relevant Pages

  • Re: Few things
    ... > here are a four more questions for the language ... > I've seen the contracts for Python: ... Decorators can do this without additional syntax. ... > def foo(): ...
    (comp.lang.python)
  • PEP thought experiment: Unix style exec for function/method calls
    ... [I'm calling this PEP thought experiment because I'm discussing language ... Suppose we could do the same for a python function - suppose we could ... def set_name: ... cexe set_name ...
    (comp.lang.python)
  • Re: A New Roguelike needs your ideas...
    ... You _could_ write a procedural program in Java and then ... complain about the language, ... written in Python by a Java programmer. ... def resaleValue: ...
    (rec.games.roguelike.development)
  • Re: Some more notes
    ... in Python you no longer need to create recursive ... on a Mathematica mis-feature *wink*). ... And as for language features; it is of my opinion that the Mathematica ... def foo: ...
    (comp.lang.python)
  • Re: Python syntax in Lisp and Scheme
    ... def a: pass ... > language you are motivated to put it OUTSIDE your primary language, ... > may well have nothing at all to do with programming. ... must be a pattern -- a pattern for growth - a pattern for growing the ...
    (comp.lang.python)

Quantcast