[comp.lang.functional] Miranda scan function

dat@ukc.ac.uk (D.A.Turner) (05/03/91)

This is a message for Miranda(*) users, many of whom probably read  this
newsgroup.   Apologies  to  other readers for using up some bandwidth in
their newsgroup.

Everyone who has a copy of Miranda release two is hereby invited to make
a  change  to the standard environment, to fix a bug reported by several
users.

There is a function in the Miranda  standard  environment  called  scan.
Here is the relevant fragment of the file .../miralib/stdenv.m
------------------------------------------------------------------------
`scan op r' applies `foldl op r' to every initial  segment  of  a  list.
For example `scan (+) 0 x' computes running sums.

> scan::(*->**->*)->*->[**]->[*]
> (definition of scan here -- 4 lines)

There is another way to explain `scan', which makes it clearer why it is
useful.    Let   s0   be   the   initial  state  of  an  automaton,  and
f::state->input->state, its state transition function - then `scan f s0'
is  a function that takes a list of inputs for the automaton and returns
the resulting list of states, starting with s0.
------------------------------------------------------------------------
(end of fragment)

In fact the definition given for scan is not quite right -  it  is  less
lazy  than  it  could  be (because it does pattern matching on the input
list earlier than it needs to).  A test case is something like
	hd (scan (+) 0 undef)
which should evaluate to 0, but the current definition gives  an  error.
This matters if you are using scan to create an interactive process.

The definition of scan in stdenv.m should be REPLACED by  the  following
code, which fixes the problem.

> scan op = g 
>           where
>           g r = (r:). rest
>                 where
>                 rest [] = []
>                 rest (a:x) = g (op r a) x

If Miranda has been installed in the usual way  you  will  need  to  get
someone  with superuser privileges to make the edit.  The `mira' command
will need to be executed once in superuser state thereafter, to recreate
the object file stdenv.x to reflect the change.

Thanks are due to Ian Holyer of Bristol University, and Andrew Malton of
Queens  University,  Ontario,  both  of  whom  reported  the mistake and
pointed out the correct definition.

The function scan is originally due to Richard Bird, and is included  in
the  Miranda  standard  environment for compatability with the excellent
text book "Introduction to Functional Programming" by  Bird  and  Wadler
(Prentice Hall 1988).

NB:- If you don't have "scan" in  your  standard  environment,  you  are
almost  certainly running Miranda release one - an old version.  In this
case you are sadly deprived, because release  two  has  many  additional
features  -  for example infinite precision integers - and is also about
twice as fast.  An upgrade to release two can be obtained for a  nominal
fee - send a message to mira-request@ukc.ac.uk for details.

David Turner
(for mira-bugs@ukc.ac.uk)

* Miranda is a trademark of Research Software Ltd.