From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA07435; Wed, 4 Feb 2004 17:09:34 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA09930 for ; Wed, 4 Feb 2004 17:09:33 +0100 (MET) Received: from thoth.sbs.de (thoth.sbs.de [192.35.17.2]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id i14G9WP05993 for ; Wed, 4 Feb 2004 17:09:32 +0100 (MET) Received: from mail3.siemens.de (mail3.siemens.de [139.25.208.14]) by thoth.sbs.de (8.11.7/8.11.7) with ESMTP id i14G9Vo23377 for ; Wed, 4 Feb 2004 17:09:31 +0100 (MET) Received: from mars.cert.siemens.de (ust.mchp.siemens.de [139.23.201.17]) by mail3.siemens.de (8.11.7/8.11.7) with ESMTP id i14G9V310497 for ; Wed, 4 Feb 2004 17:09:31 +0100 (MET) Received: from uranus.cert.siemens.de (mhpa5mac.mchp.siemens.de [139.23.201.138]) by mars.cert.siemens.de (8.12.11/8.12.11/$SiemensCERT: mail/cert.mc.pre,v 1.56 2003/11/06 20:07:28 ust Exp $) with ESMTP id i14G9VSH015016 for ; Wed, 4 Feb 2004 17:09:31 +0100 (CET) content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable X-MimeOLE: Produced By Microsoft Exchange V6.0.6487.1 Subject: [Caml-list] Doing the transition from imperative to functional (and some other Q) Date: Wed, 4 Feb 2004 17:09:30 +0100 Message-ID: Thread-Topic: Doing the transition from imperative to functional (and some other Q) Thread-Index: AcPrOUWgkPOWMfylR0GS36eMNIyCxg== From: "Christian Schaller" To: X-Loop: caml-list@inria.fr X-Spam: no; 0.00; schaller:01 schaller:01 buffer:01 buffer:01 prev:01 prev:01 char:01 calc:01 char:01 calc:01 believer:99 disappoint:99 arrays:01 chris:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hello, While trying to implement a kind of CRC algorithm used for Expert Witness (http://www.asrdata.com/SMART/whitepaper.html) I was facing a very challenging task to implement the algorithm at the bottom of above link in OCaml. Well, my first try was just 1:1 translation, so I ended up with: let crc buffer buffer_size prev_key =3D let b =3D ref (prev_key land 0xffff) in let d =3D ref ((prev_key lsr 16) land 0xffff) in for i =3D 0 to buffer_size - 1 do b :=3D !b + int_of_char buffer.[i]; d :=3D !d + !b; if i <> 0 && ((i mod 0x15b0 =3D 0) || (i =3D buffer_size - 1)) then begin b :=3D !b mod 0xfff1; d :=3D !d mod 0xfff1; end done; !d lsl 16 lor !b After I decided that there has to be a functional way for this, too, (hate typing all those ! and :=3D )I came up with the following version: let crc2 buffer prev_key =3D let buffer_size =3D List.length buffer in let b =3D prev_key land 0xffff in let d =3D (prev_key lsr 16) land 0xffff in let calc (i, b,d) ch =3D let b =3D b + int_of_char ch in let d =3D d + b in if i <> 0 && ((i mod 0x15b0 =3D 0) || (i =3D buffer_size - 1)) then (i+1, b mod 0xfff1, d mod 0xfff1) else (i+1, b,d) in let _, b,d =3D List.fold_left calc (0, b,d) buffer in d lsl 16 lor b Though I like the power of fold, I have the feeling that the expressiveness suffers somewhat. Maybe there's some more elegant version of it? Any comments? Now comes the funny part. Actually, right now I'm not sure how to represent the buffer variable internally. As you can see, in the first part I chose string, because of the .[] operator. However, I cannot use string, because this data type doesn't have any fold function. Right now I do not want to make a decision about the data type used, except that I know that it must be a sequence of characters. Though I am a strong believer in static typing, above versions disappoint me heavily because of type restrictions. Is there any way to write above version on arbitrary sequences like Arrays, Lists, Strings? Actually, I would have preferred the buffer data type because I can use that one for fast reading of a file. However, I cannot fold over a buffer :( Any enlightening hints? TIA, Chris ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners