[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Ascander Suarez <suarez@u...> |
| Subject: | Re: vector dot multiply |
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.
*)