caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Beginner's question.
@ 2005-10-27 13:30 Alexander A. Vlasov
  2005-10-27 13:41 ` [Caml-list] " Luc Maranget
  2005-10-27 13:50 ` Jean-Christophe Filliatre
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander A. Vlasov @ 2005-10-27 13:30 UTC (permalink / raw)
  To: caml-list

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.

I understand that I missed something, so please tell me what.
and... thank you for your patience 8)



-- 
Best regards,
Alexander A. Vlasov.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Beginner's question.
  2005-10-27 13:30 Beginner's question Alexander A. Vlasov
@ 2005-10-27 13:41 ` Luc Maranget
  2005-10-27 14:40   ` Damien Doligez
  2005-10-27 13:50 ` Jean-Christophe Filliatre
  1 sibling, 1 reply; 4+ messages in thread
From: Luc Maranget @ 2005-10-27 13:41 UTC (permalink / raw)
  To: Alexander A. Vlasov; +Cc: caml-list

> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Beginner's question.
  2005-10-27 13:30 Beginner's question Alexander A. Vlasov
  2005-10-27 13:41 ` [Caml-list] " Luc Maranget
@ 2005-10-27 13:50 ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 4+ messages in thread
From: Jean-Christophe Filliatre @ 2005-10-27 13:50 UTC (permalink / raw)
  To: Alexander A. Vlasov; +Cc: caml-list


Alexander A. Vlasov writes:
 > 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.
 > 
 > 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)

"function"  defines a  function over  an anonymous  argument  which is
immediatly  filtered  by the  patterns.   So  your  function mapm  has
actually  3 arguments,  the 3rd  of which  is a  pattern. I  guess you
intended  to do some  pattern matching  on the  two arguments  of your
function, which would be

	let rec mapm func list = match func, list with
	  | _, [] -> []
	  | _, head :: tail -> func head :: mapm func tail

but there is no need to do some pattern matching on the function, so

	let rec mapm func = function
	  | [] -> []
	  | head : :tail -> func head :: mapm func tail

would equally  do, which is  (regardless of the evaluation  order) the
code of List.map that you can find in the ocaml standard library.

I you don't mind  a comment: I think there is also  a mailing list for
ocaml beginners.

Best regards,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Beginner's question.
  2005-10-27 13:41 ` [Caml-list] " Luc Maranget
@ 2005-10-27 14:40   ` Damien Doligez
  0 siblings, 0 replies; 4+ messages in thread
From: Damien Doligez @ 2005-10-27 14:40 UTC (permalink / raw)
  To: caml-list

On Oct 27, 2005, at 15:41, Luc Maranget wrote:

> I sugest that, to begin with caml, you stick to explict
> match constructs, avoiding matchings introduced by function

As a seasoned OCaml programmer that's what I do anyway, because it
makes my life easier when using the debugger...

-- Damien


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-10-27 14:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-27 13:30 Beginner's question Alexander A. Vlasov
2005-10-27 13:41 ` [Caml-list] " Luc Maranget
2005-10-27 14:40   ` Damien Doligez
2005-10-27 13:50 ` Jean-Christophe Filliatre

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