deech_99 | 1 Dec 01:29
Favicon

Pattern matching on arrays

Ocaml offers pattern matching on arrays, but it doesn't seem to behave
like list pattern-matching.

For instance, the following, where 'x' is the first element and '_' is
the rest of the array doesn't work:
# match [|1;2;3|] with [|x;_|] -> x;;

Characters 0-33:

match [|1;2;3|] with [|x;_|] -> x;;

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Warning P: this pattern-matching is not exhaustive.

Here is an example of a value that is not matched:

[| |]

Exception: Match_failure ("", 100, -408).

Is there some way to do this in Ocaml?
-deech

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Search Ads

Get new customers.

List your web site

in Yahoo! Search.

Yahoo! Groups

Cats Group

Join a group for

cat owners like you

.

__,_._,___
deech_99 | 1 Dec 01:40
Favicon

Re: Pattern matching on arrays

The only solution I can come up with is to convert the array to a list
and then pattern-match like so:
match (Array.to_list [|1;2;3|]) with
x :: [] -> x
|[] -> -1

But this doesn't seem particularly efficient or elegant.

-deech

--- In ocaml_beginners <at> yahoogroups.com, "deech_99" <deech_99 <at> ...> wrote:
>
> Ocaml offers pattern matching on arrays, but it doesn't seem to behave
> like list pattern-matching.
>
> For instance, the following, where 'x' is the first element and '_' is
> the rest of the array doesn't work:
> # match [|1;2;3|] with [|x;_|] -> x;;
>
> Characters 0-33:
>
> match [|1;2;3|] with [|x;_|] -> x;;
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Warning P: this pattern-matching is not exhaustive.
>
> Here is an example of a value that is not matched:
>
> [| |]
>
> Exception: Match_failure ("", 100, -408).
>
> Is there some way to do this in Ocaml?
> -deech
>

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yahoo! Groups

Join people over 40

who are finding ways

to stay in shape.

Best of Y! Groups

Discover groups

that are the best

of their class.

.

__,_._,___
Richard Jones | 1 Dec 12:58
Favicon

Re: Pattern matching on arrays

On Mon, Dec 01, 2008 at 12:33:31AM -0000, deech_99 wrote:
> Ocaml offers pattern matching on arrays, but it doesn't seem to behave
> like list pattern-matching.
>
> For instance, the following, where 'x' is the first element and '_' is
> the rest of the array doesn't work:
> # match [|1;2;3|] with [|x;_|] -> x;;

You can't do what you want to do.

The representation of lists (as singly-linked lists) allows you to
write:

match xs with x :: _ -> x

because that matches on the first cell of the list and binds 'x' to
the head and '_' to the tail. In C the equivalent would be:

struct list {
void *elem; /* bound to 'x' */
struct list *next; /* bound to '_' */
};

Arrays are represented as a flat array of elements in memory (just
like arrays in C). There is no head or tail of an array, only
elements. Thus:

match as with [| x; y |] -> ...

matches a two element array and binds 'x' to the first element and 'y'
to the second element. There is no constructor pattern-matching form
as with lists because there is no constructor / cons cell involved in
the representation.

Rich.

--
Richard Jones
Red Hat

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Search Ads

Get new customers.

List your web site

in Yahoo! Search.

Check out the

Y! Groups blog

Stay up to speed

on all things Groups!

.

__,_._,___
Florent Monnier | 1 Dec 01:53
X-Face

Re: Pattern matching on arrays

>   match [|1;2;3|] with [|x;_|] -> x;;

here you are trying to make match together an array with 3 elements
with another array of only 2 elements.

another point is that the length of an array is an information which is 
totally absent from the type system, so if you do this :

# match [|1;2;3|] with
  | [|x;_|] -> x
  | _ -> 0
  ;;
- : int = 0

it does infere properly but the default glob case handles the array.

if you handle properly an array of length 3 in the cases, you still have to 
put a default case in order to avoid a warning:

# match [|1;2;3|] with
  | [|x;_;_|] -> x
  | _ -> 0
  ;;
- : int = 1

-- 
Cheers
Florent

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest <at> yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured <at> yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe <at> yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Gmane