Re: vector dot multiply

Ascander Suarez (suarez@usb.ve)
Thu, 08 Jun 1995 19:02:53 -0400

Message-Id: <9506082310.AA02484@shaddam.usb.ve>
To: U-E59264-Osman Buyukisik <osman.buyukisik@ae.ge.com>
Subject: Re: vector dot multiply
In-Reply-To: Your message of "Thu, 08 Jun 1995 13:16:29 -0400."
<199506081716.NAA01690@thomas.ge.com>
Date: Thu, 08 Jun 1995 19:02:53 -0400
From: Ascander Suarez <suarez@usb.ve>

Concerning your second question:

> ...
> Also, is there a similar construct to Haskell array/list comprehensions?

There is indeed one construct called streams.

Ascander (suarez@usb.ve)

---------- streamExamples.ml -----------

(* A stream of natural numbers *)

let rec generate f b = [< 'b; (generate f (f b)) >];;

let nats = generate succ 0;;

(* With this definition, the stream natS
is the structure [< '0; '1; '2; ... >]
*)

(* A stream of Fibonacci numbers needs two generators
and can be defined as:
*)

let rec generate2 f b1 b2 = [< 'b1; (generate2 f b2 (f b1 b2)) >];;

let fibs = generate2 (prefix +) 1 1;;

(* Finally, primes can be computed as follows:
*)

let rec filter n =
function [< 'm; s >] ->
if m mod n = 0 then filter n s
else [< 'm; (filter n s) >];;

let rec scieve = function [< 'm; s >] -> [< 'm; scieve(filter m s) >];;

let primes = [< '1; scieve (generate succ 2) >];;

(*
Notice that streams in Caml light are a little bit surprising in that
for any (big) stream s and any integer n, after

let s' = (function [< 'x1; 'x2; ... 'xn; restOfStream >] -> restOfStream) s;;

the streams s and s' are the same.

*)