By on 7/1/2010 12:24 PM ()

Thanks Brian, your solve made me change my answer in Stackoverflow.

EDIT: yes, I think defining the new type is better.

But I ask you now, how do I do the matchs I was used to:

1
2
3
4
5
6
7
let contains (position: int*Piece) (board: Position) = 
    let rec loop p board =
        match board with
            | [] -> false
            | hd::tl -> if hd = position then true else loop p tl
    loop position board 

In this case I get the board underline, in the call of "loop", with the fallow error message: This expression was expected to have type (int * Piece) list but here has type Position.

Thank you,

PD

By on 7/1/2010 12:38 PM ()

Now it will be

match board with

| Position([]) -> ...

| Position(h::t) -> ...

or alternatively (better)

let contains ... (Position(board)) =

where you pattern-match in the function header and bind 'board' just to the list interior contents of the position (does that make sense?)

By on 7/1/2010 12:44 PM ()

To be honest with you, it makes more or less sense...

What I got from your "better" answer was that with | Position([]) I'm doing two pattern-matching here - the first is filtering by Positions and the second is internally filtering if the list is empty or not...

Writing the pattern-match function header is something like mathematically change a expression ab + ac to a(b + c)... That was not the best metaphor but.. :) I believe I understand the concept.

Thanks very much Brian, you helped me a lot!

PD

By on 7/1/2010 1:09 PM ()

You haven't defined a new type, just a "type alias". "Position" and "list<int * Piece>" are interchangeable.

If a type alias is satisfactory, then you're done. You might annotate types, e.g.

let board1 : Position = ...

if you like.

If you really want a new type for abstraction/added type safety/documentation, then a common strategy is to use a single case discriminated union

type Position = Position of list<int * Piece>

let board1 = Position( ... )

By on 7/1/2010 12:19 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper