[m-users.] [Beginner] Optimally representing a diamond-shaped grid
Volker Wysk
post at volker-wysk.de
Sat Oct 15 21:43:42 AEDT 2022
Am Samstag, dem 15.10.2022 um 15:30 +0530 schrieb Razetime:
> I have a grid like follows:
>
> 1
> 2 3 4
> 5 6 7 8 9
> A B C
> D
>
> for a pointer that spawns on any position on this grid, the possible
> options are up, down, left and right, which move 1 step in each of
> those directions. If a direction is given and there is no spot in that
> direction, the point should remain in place.
>
> My first idea is exhaustively defining all the possible moves like so:
>
> :- type dir ---> u;d;l;r.
>
> :- pred(character:in,dir::in,character::out) is det.
> move('5',u,'5').
> move('5',d,'5').
> move('5',l,'5').
> move('5',r,'6').
> % and so on.
>
> What could be a more idiomatic method to accomplish this movement in Mercury?
I couldn't find a fitting translation of "idiomatic". But I'd try it as
follows.
I'd save the grid in a list of lists:
:- func grid_ll = list(list(char)).
grid_ll =
[['x', 'x', '1', 'x', 'x'],
['x', '2', '3', '4', 'x'],
...
].
Then I'd use the array2d library an make an array out of it:
Grid = array2d(grid_ll)
I'd get the coordinates of the start element by searching the array. From
the coordinates and from the array element 'x', I'd get to know if a move in
the specified direction is possible. If so, looking up the array at the new
coordinates, gets the target element.
Cheers,
Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20221015/038a6433/attachment.sig>
More information about the users
mailing list