caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* a problem about type recongnition
@ 2009-02-22  6:27 Su Zhang
  2009-02-22  9:01 ` [Caml-list] " blue storm
       [not found] ` <a58674cc0902212303w70a8b5eas956ef2a91968fb@mail.gmail.com>
  0 siblings, 2 replies; 3+ messages in thread
From: Su Zhang @ 2009-02-22  6:27 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1133 bytes --]

Hi all,

I have a problem while executing ocaml, did you see any problem in my code?
and do I need to give each variable a type before I use it ?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



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



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?

thank you
-- 
Su Zhang
PHD Student
Computer Information and Science
Kansas State University

[-- Attachment #2: Type: text/html, Size: 4882 bytes --]

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

* Re: [Caml-list] a problem about type recongnition
  2009-02-22  6:27 a problem about type recongnition Su Zhang
@ 2009-02-22  9:01 ` blue storm
       [not found] ` <a58674cc0902212303w70a8b5eas956ef2a91968fb@mail.gmail.com>
  1 sibling, 0 replies; 3+ messages in thread
From: blue storm @ 2009-02-22  9:01 UTC (permalink / raw)
  To: Su Zhang; +Cc: caml-list

You need parenthesis around (n+1), and it's "if m>first(element ) &&
n< second(element)" : "and" is an OCaml keyword.

There is a beginner list (
http://groups.yahoo.com/group/ocaml_beginners ) that would probably be
more suited to those questions.

On 2/22/09, Su Zhang <westlifezs@gmail.com> wrote:
> Hi all,
>
> I have a problem while executing ocaml, did you see any problem in my code?
> and do I need to give each variable a type before I use it ?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
>
>
>
> 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))
>
>
>
> 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?
>
> thank you
> --
> Su Zhang
> PHD Student
> Computer Information and Science
> Kansas State University
>


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

* Fwd: [Caml-list] a problem about type recongnition
       [not found]   ` <e2842950902220647m655729dew918962deec232c1@mail.gmail.com>
@ 2009-02-22 14:50     ` Su Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Su Zhang @ 2009-02-22 14:50 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 4346 bytes --]

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


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 <wneumann@gmail.com>

 2009/2/22 Su Zhang <westlifezs@gmail.com>
>
>> 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<http://groups.yahoo.com/group/ocaml_beginners/> .
>  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

[-- Attachment #2: Type: text/html, Size: 9509 bytes --]

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

end of thread, other threads:[~2009-02-22 14:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-22  6:27 a problem about type recongnition Su Zhang
2009-02-22  9:01 ` [Caml-list] " blue storm
     [not found] ` <a58674cc0902212303w70a8b5eas956ef2a91968fb@mail.gmail.com>
     [not found]   ` <e2842950902220647m655729dew918962deec232c1@mail.gmail.com>
2009-02-22 14:50     ` Fwd: " Su Zhang

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