From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: by yquem.inria.fr (Postfix, from userid 18041) id 5F950BB9A; Thu, 27 Oct 2005 15:41:14 +0200 (CEST) Date: Thu, 27 Oct 2005 15:41:14 +0200 To: "Alexander A. Vlasov" Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Beginner's question. Message-ID: <20051027134114.GB12868@yquem.inria.fr> References: <1130419808.12902.13.camel@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1130419808.12902.13.camel@localhost.localdomain> User-Agent: Mutt/1.5.9i From: maranget@yquem.inria.fr (Luc Maranget) X-Spam: no; 0.00; caml-list:01 beginner's:01 maranget:01 maranget:01 ocaml:01 rec:01 rec:01 mismatch:01 transformers:01 avoiding:01 matchings:01 caml-list:01 beginner's:01 ocaml:01 beginners:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=ALL_TRUSTED autolearn=disabled version=3.0.3 > Hello. > > I'm making my first steps in OCaml and trying (just for expirience) to > implement a simple function which applies given function to all elements > of given list. > I successfully did it using if/then/else > > let rec map func lst = > if lst = [] then [] > else (func (List.hd lst)) :: (map func (List.tl lst)) > ;; > > But I can't do it using pattern matching -- following function doesn't > work > > let rec mapm func lst = function > | ( _, [] ) -> [] > | ( _, head::tail ) -> (func head) :: (mapm func tail) > ;; > > I get type mismatch error: characters 39-55: > This expression has type 'a * 'b list -> 'c list but is here used with > type 'c list > > But wait, ``map func (List.tl lst)'' in first example has the same > type, isn't it? > Both right parts in first and second example are, say, transformers from > a tuple ( something, list-of-something-else ) to list-of-something-else. > Basically no, your function is defined as taking three arguments func, lst and one anonymous argument to be matched, where, in fact, you want to match over 'lst'. I sugest that, to begin with caml, you stick to explict match constructs, avoiding matchings introduced by function let rec mapm func lst = match lst with | [] -> [] | head::tail -> (func head) :: (mapm func tail) ;; Oh well, and with function : let rec mapm func = function | [] -> [] | head::tail -> (func head) :: (mapm func tail) ;; > > > > -- > Best regards, > Alexander A. Vlasov. > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- Luc Maranget