caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* from if...else to pattern matching
@ 2010-10-02 14:04 ben kuin
  2010-10-02 14:22 ` [Caml-list] " Philip
  2010-10-02 17:54 ` Jon Harrop
  0 siblings, 2 replies; 5+ messages in thread
From: ben kuin @ 2010-10-02 14:04 UTC (permalink / raw)
  To: caml-list

hi
I try to transform an if-else clause into pattern matching, I think
I've tried a lot of approaches, but apperently I'm doing something
fundemently wrong.


~~~~~~~~~
(** Define behaviors against a constang   'x =< c' with if...else and
pattern matching.
I want to see how to match a value against funtion **)

(* defining a  '=<' operator *)
let (=<) a b = if a < b then true else if a == b then true else false;;

let if_test c  =
if ( 4 =< c )then
  true
else
  false
;;

(* and now use the operator in pattern matching * )
let match_test c =
  match  ( _ =< c ) (* pseudocode *)
    | 4 -> true
    | _ -> false
;;
~~~~~~~~~~~~~~~~~~~~~~~~~~~


thanks in advance
ben


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

* Re: [Caml-list] from if...else to pattern matching
  2010-10-02 14:04 from if...else to pattern matching ben kuin
@ 2010-10-02 14:22 ` Philip
  2010-10-02 14:47   ` ben kuin
  2010-10-02 17:54 ` Jon Harrop
  1 sibling, 1 reply; 5+ messages in thread
From: Philip @ 2010-10-02 14:22 UTC (permalink / raw)
  To: caml-list

On Sat, 2010-10-02 at 16:04 +0200, ben kuin wrote:
> hi
> I try to transform an if-else clause into pattern matching, I think
> I've tried a lot of approaches, but apperently I'm doing something
> fundemently wrong.
> 
> 
> ~~~~~~~~~
> (** Define behaviors against a constang   'x =< c' with if...else and
> pattern matching.
> I want to see how to match a value against funtion **)
> 
> (* defining a  '=<' operator *)
> let (=<) a b = if a < b then true else if a == b then true else false;;
> 
> let if_test c  =
> if ( 4 =< c )then
>   true
> else
>   false
> ;;
> 
> (* and now use the operator in pattern matching * )
> let match_test c =
>   match  ( _ =< c ) (* pseudocode *)
>     | 4 -> true
>     | _ -> false
> ;;
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> 
> thanks in advance
> ben
> 

whoa, thats hard,
try:

# let t x = match x with
  a when x<=4 -> true
  | _ -> false;;

btw, there is a beginner-list and irc-channel,
Philip

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




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

* Re: [Caml-list] from if...else to pattern matching
  2010-10-02 14:22 ` [Caml-list] " Philip
@ 2010-10-02 14:47   ` ben kuin
  2010-10-02 16:09     ` Hezekiah M. Carty
  0 siblings, 1 reply; 5+ messages in thread
From: ben kuin @ 2010-10-02 14:47 UTC (permalink / raw)
  To: Philip, caml-list

>
> # let t x = match x with
>  a when x<=4 -> true
>  | _ -> false;;
>

ok, I had a similar attempt with

    let tt x = function
    a when x<=4 -> true
    | _ -> false;;

but that gave me the following (scary  -> 'a -> ) signature

     val tt : int -> 'a -> bool = <fun>

so I stopped

thanks anyway


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

* Re: [Caml-list] from if...else to pattern matching
  2010-10-02 14:47   ` ben kuin
@ 2010-10-02 16:09     ` Hezekiah M. Carty
  0 siblings, 0 replies; 5+ messages in thread
From: Hezekiah M. Carty @ 2010-10-02 16:09 UTC (permalink / raw)
  To: ben kuin; +Cc: Philip, caml-list

On Sat, Oct 2, 2010 at 10:47 AM, ben kuin <benkuin@gmail.com> wrote:
>>
>> # let t x = match x with
>>  a when x<=4 -> true
>>  | _ -> false;;
>>
>
> ok, I had a similar attempt with
>
>    let tt x = function
>    a when x<=4 -> true
>    | _ -> false;;
>
> but that gave me the following (scary  -> 'a -> ) signature
>
>     val tt : int -> 'a -> bool = <fun>
>
> so I stopped
>
> thanks anyway
>

This seems to be a common beginner mistake (hence the reference to the
beginner's list).  You defined tt as a function which takes two
arguments, x and another matched by "function" - which tt ignores the
value of (a when x <= 4).  The pattern-matched value is never used,
therefore it can be any type.

Hope this helps,

Hez


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

* RE: [Caml-list] from if...else to pattern matching
  2010-10-02 14:04 from if...else to pattern matching ben kuin
  2010-10-02 14:22 ` [Caml-list] " Philip
@ 2010-10-02 17:54 ` Jon Harrop
  1 sibling, 0 replies; 5+ messages in thread
From: Jon Harrop @ 2010-10-02 17:54 UTC (permalink / raw)
  To: 'ben kuin', caml-list

Note that you are using physical equality (==) and not structural equality
(=).

Also, you can always rewrite:

  if <pred> then true else false

more simply as:

  <pred>

> -----Original Message-----
> From: caml-list-bounces@yquem.inria.fr [mailto:caml-list-
> bounces@yquem.inria.fr] On Behalf Of ben kuin
> Sent: 02 October 2010 15:05
> To: caml-list@inria.fr
> Subject: [Caml-list] from if...else to pattern matching
> 
> hi
> I try to transform an if-else clause into pattern matching, I think
> I've tried a lot of approaches, but apperently I'm doing something
> fundemently wrong.
> 
> 
> ~~~~~~~~~
> (** Define behaviors against a constang   'x =< c' with if...else and
> pattern matching.
> I want to see how to match a value against funtion **)
> 
> (* defining a  '=<' operator *)
> let (=<) a b = if a < b then true else if a == b then true else false;;
> 
> let if_test c  =
> if ( 4 =< c )then
>   true
> else
>   false
> ;;
> 
> (* and now use the operator in pattern matching * )
> let match_test c =
>   match  ( _ =< c ) (* pseudocode *)
>     | 4 -> true
>     | _ -> false
> ;;
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> 
> thanks in advance
> ben
> 
> _______________________________________________
> 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


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

end of thread, other threads:[~2010-10-02 17:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-02 14:04 from if...else to pattern matching ben kuin
2010-10-02 14:22 ` [Caml-list] " Philip
2010-10-02 14:47   ` ben kuin
2010-10-02 16:09     ` Hezekiah M. Carty
2010-10-02 17:54 ` Jon Harrop

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