caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: maranget@yquem.inria.fr (Luc Maranget)
To: "Alexander A. Vlasov" <zulu@galaradio.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Beginner's question.
Date: Thu, 27 Oct 2005 15:41:14 +0200	[thread overview]
Message-ID: <20051027134114.GB12868@yquem.inria.fr> (raw)
In-Reply-To: <1130419808.12902.13.camel@localhost.localdomain>

> 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


  reply	other threads:[~2005-10-27 13:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-27 13:30 Alexander A. Vlasov
2005-10-27 13:41 ` Luc Maranget [this message]
2005-10-27 14:40   ` [Caml-list] " Damien Doligez
2005-10-27 13:50 ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051027134114.GB12868@yquem.inria.fr \
    --to=maranget@yquem.inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=zulu@galaradio.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).