dear friends

I thought I got the hang of things and reckoned I could iterate a few things shortly however while

1
2
3
4
5
6
7
8
9
10
11
12
13
let in3cube x = x / 16 <> 3 && x / 4 % 4 <> 3 && x % 4 <> 3
let rec check i j =
if j < i then 
if (knightpath.[j] = knightpath.) then
knightpath. <- -1
else
check i (j+1)
for i in 1..max-1 do
if (knightpath.[i - 1] = -1) then
knightpath. <- -1;
else
knightpath. <- knightreaches.[knightpath.[i - 1]].[0];
check i 0

gives me the expected result:

1
2
3
4
5
6
7
[|0; 6; 15; 43; 45; 63; 57; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1|]

trying to iterate through more possible values stored in th array knightreaches

I got the following error messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let rec knightpaths ii kk = [
let knightpath = Array.zeroCreate 10;// max;
knightpath.[0] <- 0
let rec check i j =
if j < i then 
if (knightpath.[j] = knightpath.) then
knightpath. <- -1
else
check i (j+1)
if (knightpath.[ii - 1] = -1) then
knightpath.[ii] <- -1;
else
knightpath.[ii] <- kk;
check ii 0
let in3cube x = x / 16 <> 3 && x / 4 % 4 <> 3 && x % 4 <> 3
if in3cube knightpath.[ii] then
yield knightpaths (ii+1) knightreaches.[knightpath.].[0]
// Error 1 Type mismatch. Expecting a 'a but given a 'a list. The resulting type would be infinite when unifying ''a' and ''a list' .... (omitted)
else
yield knightpath
]

or without recursive 'rec':

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let knightpaths ii kk = [
let knightpath = Array.zeroCreate 10;// max;
knightpath.[0] <- 0
let rec check i j =
if j < i then 
if (knightpath.[j] = knightpath.) then
knightpath. <- -1
else
check i (j+1)
if (knightpath.[ii - 1] = -1) then
knightpath.[ii] <- -1;
else
knightpath.[ii] <- kk;
check ii 0
let in3cube x = x / 16 <> 3 && x / 4 % 4 <> 3 && x % 4 <> 3
if in3cube knightpath.[ii] then
yield knightpaths (ii+1) knightreaches.[knightpath.].[0]
//Error 1 The value or constructor 'knightpaths' is not defined. ....(omitted)
else
yield knightpath
]

so I'm back to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let knightpaths ii kk = [
let knightpath = Array.zeroCreate 10;// max;
knightpath.[0] <- 0
let rec check i j =
if j < i then 
if (knightpath.[j] = knightpath.) then
knightpath. <- -1
else
check i (j+1)
if (knightpath.[ii - 1] = -1) then
knightpath.[ii] <- -1;
else
knightpath.[ii] <- kk;
check ii 0
//let in3cube x = x / 16 <> 3 && x / 4 % 4 <> 3 && x % 4 <> 3
//if in3cube knightpath.[ii] then
// yield knightpaths (ii+1) knightreaches.[knightpath.].[0]
//else
yield knightpath
]

let knigtjumpies = [
for kk in knightreaches.[0] do
yield knightpaths 1 kk
]

printfn "" 
for kj in knigtjumpies do
printfn "%A" kj

which gives me only the first possibilities:

1
2
3
4
5
6
7
8
9
10
11
[[|0; 6; 0; 0; 0; 0; 0; 0; 0; 0|]]

[[|0; 9; 0; 0; 0; 0; 0; 0; 0; 0|]]

[[|0; 18; 0; 0; 0; 0; 0; 0; 0; 0|]]

[[|0; 24; 0; 0; 0; 0; 0; 0; 0; 0|]]

[[|0; 36; 0; 0; 0; 0; 0; 0; 0; 0|]]

[[|0; 33; 0; 0; 0; 0; 0; 0; 0; 0|]]

correctly

currently just a bit stumped perhaps due to my current state of mind and time.

just after the result shown near the top I reckoned I would get simular lines by now showing me all the possibilities uptill the condition (knightjump path getting outside the 3 by 3 by 3 cube)

sorry for loosing the indentation of the code for some reason "Formatted" returned to "paragraph" and now showing "normal"

i hope it is still understandable.

darn I see [ i ] stands for a light here, having to figure out how to paste code on every new forum?!

just a note I reckon to move the array knightpath outside the knightpaths function, and I need to formulate a copy constructor to yield the result.

other attempts I just got the last possibility mentioned several (6) times.

Further I hope to find when moving to 2010 (when released) to be able to set breakpoint on a code of line to enable debugging.

now it holds on an entire function and skips past the function in stead of allowing me to debug the function.

By on 6/10/2009 10:41 AM ()

If you want your code to appear correctly, use these tags:

{code language="F#"}
<ForumPost.Replace("{","[").Replace("}","]")>
{/code}

It's hard to see exactly what your code does, but I noticed something at the end of your knightpaths function.

The

1
if in3cube knightpath.[ii]...

statement yields two different things at each branch. The first branch yields knightpaths and the second branch yields knightpath. The former is a list of arrays, but the second is an array. Outputting different kinds of data in different if/then/else branches is a no-no. The same goes for match statements, of course.

Instead of having the first branch spit out the entire list of arrays as a single entry in your list, try using

1
yield! knightpaths (ii+1) ...

instead of

1
yield knightpaths (ii+1) ...

to have the first branch spit out lists one-by-one.

P.S. Tip: try not to use library function names when naming your values. Naming a value max may lead to weird bugs later.

By on 6/11/2009 1:15 AM ()

{code language="F#"}
<ForumPost.Replace("{","[").Replace("}","]")>
{/code}

Sorry about this. I was being too cryptic for my own good. I meant this:

{code language="F#"}
.. insert code here ..
{/code}

replace { } by [ ]

By on 6/11/2009 4:11 AM ()

edited my former post thus, but still the font tags I didn't enter.

anyway the code now looks ok. I tried yesterday also code tags in square brackets but hadn't an idea of the language argument.

from the F#-specs I got the difference between yield! (for computation) and yield (for result)

thnx.

[;)]STRANGE code tag in square brackets generates font tags in the following code ???[:O]

figured out the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let knecht ii kk jj: array<int> =
   let knightpath = Array.zeroCreate 10
   let rec knechtp ii kk =
      knightpath.[ii] <- kk
      check ii 0
      if (ii < 9 && knightpath.[ii] <> -1 && in3cube knightpath.[ii]) then
         knechtp (ii+1) knightreaches.[kk].[jj]
      else
         knightpath.[ii + 1] <- -1
   knightpath.[ii] <- 0;
   knechtp (ii+1) kk
   let mutable len = 1
   while len < 10 && knightpath.[len] <> -1 do
      len <- len + 1; 
   let knightres = Array.zeroCreate len
   array.Copy(knightpath,knightres,len)
   knightres

let knigtjumpies = [
   for kk in knightreaches.[0] do
      yield knecht 0 kk 0
      yield knecht 0 kk 1
   ]

to give me the knightjumpies:

1
2
3
4
5
6
7
8
9
10
11
12
[|0; 6; 15|]
[|0; 6; 30|]
[|0; 9; 15|]
[|0; 9; 27|]
[|0; 18; 27|]
[|0; 18; 42; 35|]
[|0; 24; 30|]
[|0; 24; 42; 35|]
[|0; 36; 42; 44|]
[|0; 36; 45|]
[|0; 33; 39|]
[|0; 33; 42; 35|]

I need but to figure out how to iterate through all possible values.

A bit strange though as I tried to change the constant '10' to the length of the array

i found knightpath.GetLength(int) I'll have no clue what the int parameter is supposed

to do there! Expected GetLength to give me 10 back! ???

By on 6/11/2009 4:58 AM ()

thanks cfern

What I intend to do is to find KnightJump Path's through an order 4 cube within the order 3 subcube, test the found values whether a semi-magic cube can be obtained, etc. which goes a bit too far here. a knightjump path looks like

1
2
3
4
5
6
7
[|0; 6; 15; 43; 45; 63; 57; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1; -1;

-1; -1; -1; -1|]

where positions are coded as p = z*16+y*4+x for convenience.

Thanks for the rule of thumb I could have guessed that is was a no-no to output different types. I'm new to the language (in fact new to functional programming, save some C# linq statments) so I'll need to dig into the difference between yield and yield!. Also familiarize me with library function names like max.

This is just a puny test project, as functional programming looks still like a good idea for the problem I encountered. And the data to knightjump I obtained in but a couple of days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let TestingArray() =
    let knightjumps = [
      yield (0,1,2)
      yield (0,2,1)
      yield (1,0,2)
      yield (1,2,0)
      yield (2,1,0)
      yield (2,0,1)
      yield (0,-1,2)
      yield (0,2,-1)
      yield (-1,0,2)
      yield (-1,2,0)
      yield (2,-1,0)
      yield (2,0,-1)
      yield (0,1,-2)
      yield (0,-2,1)
      yield (1,0,-2)
      yield (1,-2,0)
      yield (-2,1,0)
      yield (-2,0,1)
      yield (0,-1,-2)
      yield (0,-2,-1)
      yield (-1,0,-2)
      yield (-1,-2,0)
      yield (-2,-1,0)
      yield (-2,0,-1)
      ]
   let add (x,y,z)
 (vx,vy,vz) = (x+vx,y+vy,z+vz)
   let order = 4
   let max = order * order * order
   let square = Array3D.create order order order 0;
   let nums = [0 .. order - 1]
   for i in nums do
      for j in nums do
         for k in nums do
            square.[i,j,k] <- i * order * order + j * order + k;
   let ok (x,y,z) = x >= 0 && x < order && y >= 0 && y < order && z >= 0 && z < order
   let posnum (x,y,z) = x * order * order + y * order + z
   let knightreach (x,y,z) = [
         for vec in knightjumps do
         let pos = add (x,y,z) vec
            if (ok pos) then
               yield posnum pos
      ]
   let knightreaches = Array.zeroCreate max;
   for i in 0..max-1 do
      knightreaches.[ i ] <- knightreach (i / (order * order) , i / order % order, i % order)
   for i in 0..max-1 do
      printfn "%02d %A" i knightreaches.[ i ]

without 'posnum' positions are ouputted, which would be ok also, and probably with F# matching could be used just as simple as the coded positions.

[:O]hope it is formatted, yet pasted it looks strange (manually corrected)

[:@]why does it looks like I am changing font!

By on 6/11/2009 3:48 AM ()
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