Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

vector dot multiply
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 1995-06-09 (10:02) From: Ascander Suarez Subject: Re: vector dot multiply
```

> ...
> 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.

*)

```