Arc Forumnew | comments | leaders | submitlogin
2 points by lojic 6139 days ago | link | parent

I took sacado's code and tweaked it a bit (Ruby's push appends and Arc's push prepends). Here's the result. If someone can get rid of my joinstr function and make the prn line closer to the Ruby version in conciseness, then I think Arc does pretty well.

For this challenge, it's important to have the output match exactly - that's why the rev is necessary and the complicated print. I do string interpolation all the time in Ruby, so I wanted to see how easy/hard it was in Arc to produce lines such as [ 1, 5, 8, 6, 3, 7, 2, 4 ]

I might as well limit the code to ArcN also.

I think the following may be the only working version of Arc code that passes this challenge so far.

Having to use point is a bit awkward, but not too bad. I particularly like the function composition such as abs:- and the more concise len.stack notation. The [ string sep _ ] notation is definitely a winner also. I'm pretty pleased with the core language so far - kudos to pg & rm.

  (def valid? (stack)
    (let q2 0
      (point return
             (each q1 (range 1 (- len.stack 1))
                   (if (or (is stack.q1 stack.q2)
                           (is (abs:- q1 q2) (abs:- stack.q1 stack.q2)))
                       (return nil)))
             t)))

  (def joinstr (lst (o sep " "))
    (if lst
        (string (car lst) (apply string (map [string sep _] (cdr lst))))
        ""))

  (def queens (stack n)
    (if (is n 8)
        (prn "[ " (joinstr (rev stack) ", ") " ]")
        (each rank (range 1 8)
              (push rank stack)
              (if (valid? stack)
                  (queens stack (+ n 1)))
              (pop stack))))

  (queens '() 0)
---

  arc> (queens '() 0)
  [ 1, 5, 8, 6, 3, 7, 2, 4 ]
  [ 1, 6, 8, 3, 7, 4, 2, 5 ]
  [ 1, 7, 4, 6, 8, 2, 5, 3 ]
  [ 1, 7, 5, 8, 2, 4, 6, 3 ]
  [ 2, 4, 6, 8, 3, 1, 7, 5 ]
  ...
  [ 8, 3, 1, 6, 2, 5, 7, 4 ]
  [ 8, 4, 1, 3, 6, 2, 7, 5 ]


2 points by map 6139 days ago | link

  (def valid? (stack)
    (let i 0
      (if (or (pos stack.0 cdr.stack)
              (some (fn(x)(++ i)(is i (abs:- x stack.0))) cdr.stack))
        nil
        t)))

  (def queens (stack n)
    (if (is n 8)
      (and (prall rev.stack "[ ") (prn " ]"))
      (for rank 1 8
        (push rank stack)
        (if (valid? stack)
            (queens stack (+ n 1)))
        (pop stack))))

  (queens '() 0)

-----

2 points by almkglor 6139 days ago | link

  (def valid? (stack)
    (let i 0
      (if (or (pos stack.0 cdr.stack)
              (some [do (++ i) (is i (abs:- _ stack.0))] cdr.stack))
        nil
        t)))

-----

1 point by map 6138 days ago | link

  (def valid? (stack)
    (let i 0
      (if (or (pos stack.0 cdr.stack)
              (some [is ++.i (abs:- stack.0 _)] cdr.stack))
        nil
        t)))

  (def queens (stack n)
    (if (is n 8)
      (do (prall rev.stack "[ ") (prn " ]"))
      (for rank 1 8
        (push rank stack)
        (if (valid? stack)
            (queens stack (+ n 1)))
        (pop stack))))

  (queens '() 0)

-----

1 point by lojic 6139 days ago | link

Nice job - thx.

-----

1 point by almkglor 6139 days ago | link

  arc> (help prall)
  (from "arc.arc")
  [fn]  (prall elts (o init ) (o sep , ))
   Prints several arguments with an initial header and separated by a
      given separator. 
Above function also exists in Arc2.

Instead of (point return ...(return ...)) try using (catch ...(throw ...)).

Final nitpick: I think it's rtm, not rm, although, well, I dunno. ^^

-----

1 point by lojic 6139 days ago | link

Have you tried prall to print the output in the OP? It's awkward.

-----

1 point by map 6139 days ago | link

  arc> (help prall)
  Error: "reference to undefined identifier: _help"

-----

1 point by nex3 6139 days ago | link

help is a macro defined on Anarki.

-----