Re: if expression and match expression

"Seung-jin Kim [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <20151009035552.GA37167@pristine>
First of all,
There was a typo in my very initial question.
my has_element1 should be

let rec has_element1 l e =
match l with
| [] -> false
| h::t -> if e = h then true else has_element1 t e;;

Anyway,, Seems everyone got my point. :-) First time to post this
group and very new to ocaml.

I did with ( ) for my second match.

utop[91]> let rec has_element2 l e =
match l with
| [] -> false
| h::t -> ( match h with
            | e -> true
	    | _ -> has_element2 t e
          )
;;


val has_element2 : 'a list -> 'b -> bool = <fun>
Characters 107-108:
Warning 11: this match case is unused.
utop[92]> has_element2 [2;3;4] 10;;
- : bool = true
utop[93]>

Still getting the same warning message with the same result. 


On 10/08 11:19 PM, Hendrik Boom [email protected] [ocaml_beginners] wrote:
> On Thu, Oct 08, 2015 at 08:12:48PM -0700, [email protected] [ocaml_beginners] wrote:
> > Code first:
> >  
> > 
> >  let rec has_element1 l e =
> > match l with
> > | [] -> false
> > | h::t -> if e = h then true else false
> > ;;
> > 
> > let rec has_element2 l e =
> > match l with
> > | [] -> false
> > | h::t -> match h with
> >           | e -> true
> >       | _ -> has_element2 (List.tl l) e
> > ;;
> > 
> >  
> > 
> >  has_element1 works as I expected. 
> > 
> >  And I rewrite has_element1 with replacing 'if expressing' with 'match. 
> > 
> >  But has_element2 always return 'true' and it gives me warning: 'Warning 11: this match case is unused'
> >  
> > 
> >  Questions: 
> > 
> >   (1) Why does my has_element2 always return true? Match expression I 
> > used in has_element2 not the same as if expression in has_element1?
> 
> How is the poor compiler to know whether the last alternative belongs 
> with the first or the second match?  Try inserting ( before the second 
> match keyword and putting its ")" where it belongs.
> 
> I think this may be a language design problem.  But the problem ends up 
> being caught in the type analysis, so it's probably only confusing.
> 
> When OCaml misbehaves I usually find it enlightening to be explicit 
> about types and parentheses.
> 
> -- hendrik

Seungjin Kim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.