From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.8 required=5.0 tests=DNS_FROM_RFC_POST, HTML_MESSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 864AFBBAF for ; Sun, 22 Feb 2009 15:50:07 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AowCAPH0oElKfVwZi2dsb2JhbACCQi6KH4cOPwEBAQoLCgcPBbEVgQWNLwEDAQOEDAY X-IronPort-AV: E=Sophos;i="4.38,250,1233529200"; d="scan'208";a="35575747" Received: from qw-out-2122.google.com ([74.125.92.25]) by mail4-smtp-sop.national.inria.fr with ESMTP; 22 Feb 2009 15:50:06 +0100 Received: by qw-out-2122.google.com with SMTP id 3so449845qwe.15 for ; Sun, 22 Feb 2009 06:50:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=M5cjIZjVYD+LfYcGVoaOCycTDsnex8o1IHYKF/NlHD0=; b=t/Q5aT03cDHXzecQS5JdaCJim0IFsYg0rKBN6ZI2USqtFopeoRmxqDfVTW04tflNnn eTa3EyjMLT89ztoR79+yuTaQ7rHKG6YpoS+iTvgEtHLyLtas7mExij/hnegNmsBlqAHf K4nb7ivP2edFsCoHrVCij3j9aUmvofvlVMf6c= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=qd4FCvFjWi3ZeZ5+DWwPuguM/X6+T/c515qXTLPo9MGkiTrzgLpoE9H54Of+u9TpMt KjdfqbsZF93HZq2J4xRe1u+1SFtGRo1iPx196ur3mte19KufstHeE74pFPPSGLRj1+1L FFsc2pyL7j9mj52e8ERDJcR5vxdpJX+or/8No= MIME-Version: 1.0 Received: by 10.229.110.14 with SMTP id l14mr1087550qcp.12.1235314205495; Sun, 22 Feb 2009 06:50:05 -0800 (PST) In-Reply-To: References: Date: Sun, 22 Feb 2009 08:50:05 -0600 Message-ID: Subject: Fwd: [Caml-list] a problem about type recongnition From: Su Zhang To: caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=0016363b810667912204638300c4 X-Spam: no; 0.00; ocaml:01 recursive:01 rebuilt:01 rebuilt:01 recursive:01 compiler:01 parens:01 ocaml:01 beginner's:01 compiler:01 parens:01 beginner's:01 2009:98 2009:98 W8:98 --0016363b810667912204638300c4 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit ---------- Forwarded message ---------- From: Su Zhang Date: 2009/2/22 Subject: Re: [Caml-list] a problem about type recongnition To: William Neumann thank you very much, because my gmail mailbox can not be verified by yahoo and I use yahoo mailbox to join the group, yet it is still pending, since it's almost the deadline for me, so would you please take a look at the following two questions if possible? 1 I would like to use a comparision function to sort my list and provide a final result, so this is the comparision function I have: let rec comparision xs ys if hd(third(xs))> hd(third(ys)) then 1 else if hd(third(xs))= hd(third(ys)) then 0 else -1 I want to use this compariasion which means the list of triples should be sorted based on the first element of the third value in each triple,yet it seems it has some problems which I really donot understand, it says the if has some problems... would you please give me some ideas, and if I can figure out this problem, can i use the sort function to sort a list and output an ordered result? like this: ys=let rec sort comparision ys ? and here ys is a list of triples. thanks 2009/2/22 William Neumann 2009/2/22 Su Zhang > >> Hi all, >> >> I have a problem while executing ocaml, did you see any problem in my >> code? >> > > Yes, a few. See below. > > >> and do I need to give each variable a type before I use it ? >> > > Generally speaking, no. There are a few cases where it needs to be done, > or it helps to specify types, but most of the time the types will be > inferred for you. > > >> my code is in the following, you can take a look at the code and the info >> of the errors >> >> >> let rec first(x, y, z)=x >> >> let rec second(x,y,z)=y >> >> let rec third(x,y,z)=z >> > > It's not an error, but there's no need for rec here. These aren't > recursive functions. > > >> let rec rebuilt anss (m,n,ys)= match anss with >> >> [] -> (m,n,ys) >> >> |element::anss -> >> >> if m>first(element ) and n< second(element) >> >> then rebuilt anss (m,n,ys) >> >> else element::(rebuild anss (m,n,ys)) >> > > Three problems here: > 1: You alternate between calling this function rebuilt and rebuild. > 2: The "and" in the fourth line should be "&&". The "and" keyword is used > for recursive definitions. > 3: You have a type error in this definition. in the first pattern, the > return type of the function is 'a * 'b * 'c, but in the else branch of the > second pattern, you return an ('a * 'b * 'c) list. > > Assuming that you want to return a list, a more idiomatic way to write this > function is: > > let rec rebuild anss ((m,n,_) as tup) = > match anss with > | [] -> [tup] > | (x,y,_)::t when m > x && n < y -> rebuild t tup > | h::t -> h::(rebuild t tup) > ;; > > Note how you can use pattern matching to eliminate the need for your first, > second, and third functions. > > Of course, you may also want to look at the functions in the standard > library's List module (in particular List.filter) which likely make writing > functions like his much easier. > > >> let rec sum anss cur n ys= match cur with >> >> None -> >> >> (match ys with >> >> []-> anss >> >> |y::ys-> >> >> if y=0 >> >> then sum(rebuild anss (n,n,0)) None n+1 ys >> >> else sum anss (Some(n, y, [y])) (n + 1) ys) >> >> >> the red area is shown has an error, and the error info is like this:"This >> expression has type int list -> 'a but is here used with type int", so would >> you please let me know if possible how can I let the compiler know the type >> of anss is a list but not an integrer? >> > > The problem here is that the part in red is being parsed as (sum ( rebuild > anss (n,n,0)) None n) + 1 ys. To fix this, you need to put the n+1 in > parens. > > Finally, questions like this are better asked on the OCaml Beginner's list . > You may want to join that list as well and use it for your questions while > you learn OCaml. > > William D. Neumann > -- Su Zhang PHD Student Computer Information and Science Kansas State University -- Su Zhang PHD Student Computer Information and Science Kansas State University --0016363b810667912204638300c4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

---------- Forwarded message ----------
From:= Su Zhang <westlifezs@gmail.com>
Dat= e: 2009/2/22
Subject: Re: [Caml-list] a problem about type recongnition
To: William N= eumann <wneumann@gmail.com>=


thank you very much, because my gmail mailbox can not be verified by y= ahoo and I use yahoo mailbox to join the group, yet it is still pending, si= nce it's almost the deadline for me, so would you please take a lo= ok at the following two questions if possible?
 
1 I would like to use a comparision function to sort my list and provi= de a final result, so this is the comparision function I have:
let rec comparision xs ys
      if hd(third(xs))> hd(third(ys))
  &nbs= p;     then 1
      else if= hd(third(xs))=3D hd(third(ys))
      &nbs= p;  then 0
      else -1
I want to use this compariasion which means the list of triples should= be sorted based on the first element of the third value in each triple,yet= it seems it has some problems which I really donot understand, it  sa= ys the if has some problems... would you please give me some ideas, and if = I can figure out this problem, can i use the sort function to sort a list a= nd output an ordered result? like this: ys=3Dlet rec sort comparision ys ? and here ys is a list of t= riples.
thanks
2009/2/22 William Neumann <= wneumann@gmail.com<= /a>>=20

2009/2/22 Su Zhang <westlifezs@gmail.com= >=20

Hi all,
 
I have a problem while executing ocaml, did you see any problem in my = code?

Yes, a few.  See below.
 
and do I need to give each variable a type before I use it ?

Generally speaking, no.  There are a few cases where it needs to = be done, or it helps to specify types, but most of the time the types will = be inferred for you.
 
my code is in the following, you can take a look at the code and the i= nfo of the errors
 

let rec first(x, y, z)=3Dx

let rec second(x,y,z)=3Dy

let rec third(x,y,z)=3Dz


It's not an error, but there's no need for rec here.  The= se aren't recursive functions.
 

 let rec rebuilt anss  (m,n,ys)=3D matc= h anss with

       =  []         =   ->       (m,n,y= s) 

|element::anss ->

         <= /span>if m>first(element ) and n< second(element)

         <= /span>then rebuilt anss (m,n,ys)

         <= /span>else element::(rebuild anss (m,n,ys))


Three problems here:
1: You alternate between calling this function rebuilt and rebuild.
2: The "and" in the fourth line should be "&&&q= uot;.  The "and" keyword is used for recursive definitions.<= /div>
3: You have a type error in this definition.  in the first patter= n, the return type of the function is 'a * 'b * 'c, but in the = else branch of the second pattern, you return an ('a * 'b * 'c)= list.

Assuming that you want to return a list, a more idiomatic way to write= this function is:

let rec rebuild anss ((m,n,_) as tup) =3D
  match anss with 
  | [] -> [tup]
  | (x,y,_)::t when m > x && n < y -> rebui= ld t tup
  | h::t -> h::(rebuild t tup)
;;

Note how you can use pattern matching to eliminate the need for your f= irst, second, and third functions.

Of course, you may also want to look at the functions in the standard = library's List module (in particular List.filter) which likely make wri= ting functions like his much easier.
 

 let rec sum anss cur n ys=3D match cur with

        &n= bsp;  None   -><= /p>

        &n= bsp;    (match ys with

[]-> anss

|y::ys->

if y=3D0

then sum(rebuil= d anss (n,n,0)) None n+1 ys

else sum anss (Some(n, y, [y])) (n + 1)= ys)

 

the red area is shown has an error, and the error info is like this:&q= uot;This expression has type int list -> 'a but is here used with ty= pe int", so would you please let me know if possible how can I let the= compiler know the type of anss is a list but not an integrer?

The problem here is that the part in red is being parsed as (sum ( reb= uild anss (n,n,0)) None n) + 1 ys.  To fix this, you need to put the n= +1 in parens. 

Finally, questions like this are better asked on the OCaml Beginner&= #39;s list .  You may want to join that list as well and use = it for your questions while you learn OCaml.

William D. Neumann



--
Su Zhang
PHD Student
Computer Infor= mation and Science
Kansas State University


--
Su Zhang
PHD Student
Computer Information = and Science
Kansas State University
--0016363b810667912204638300c4--