|
From: | Elias Mårtenson |
Subject: | Re: [Bug-apl] Error handling and IO |
Date: | Wed, 22 Jun 2016 14:27:38 +0800 |
X←¯1+?20⍴2
X
1 1 1 0 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 1
+/(X,1)≠(1↑X),X
8
Well, 8 is the answer to the question, it says nothing on how to solve that puzzle.
A point + on your QI(fr) or IQ(us).
eg: Keep your mind on the question, not on the solution.
if you can elaborate on the 8 here, please do explain.
well, I will dissect your code and try to figure out by myself.
well, no, finally just tell me how that work.
many thanks,
Xtian.
On 2016-06-22 00:47, Elias Mårtenson wrote:
All solutions are available here: https://www.go-hero.net/jam/16
But the relevant core of my solution is here (minus all the file parsing stuff):
+/(X,1)≠(1↑X),X
Where X is an array of 1 and 0 representing the state of the pancake stack.
The code should be pretty clear, but please let me know if you need help understanding it and I'll be happy to explain.
For reference, the full code is here:
∇Z←solve_case X
Z ← +/(X,1)≠(1↑X),X
∇
∇Z←IN read_case I ;line
line ← ⎕UCS ¯1↓FIO∆fgets IN
⎕←"Case #",(⍕I),": ",(⍕solve_case line='+')
Z←⍬
∇
∇solve FILENAME ;fd;n;s
fd ← "r" FIO∆fopen FILENAME
n ← ⍎ ⎕UCS ¯1 ↓ FIO∆fgets fd
({(⍵+1)⊣fd read_case ⍵}⍣n) 1
FIO∆fclose fd
∇
Regards,
Elias
On 22 June 2016 at 12:36, Christian Robert <address@hidden <mailto:address@hidden>> wrote:
Can I see your solution to the stack of pancakes ?
I wrote a Lambda (at that time, 2 months ago) to reverse some pancakes off the top of the stack and experimented with that.
No obvious solution came to me.
I only need the mechanics/algorithm, not the READ from file part (but you can provide it if relevant).
thanks,
Xtian.
On 2016-04-10 07:48, Elias Mårtenson wrote:
Yesterday, I participated in the Google Code Jam (programming competition). For one of the tasks, APL was a very good fit. The question is here: https://code.google.com/codejam/contest/6254486/dashboard#s=p1
My solution (which I will not post right now, since one of you might want to give it a shot first) was terse and simple. A very simple APL _expression_.
However, reading the input a file and formatting the result took many lines of very ugly code.
This attempt at using APL to solve a real-world programming problem illustrated two separate issues that, needs to be handled:
Firstly, the FILE_IO library is way too low-level. For example, in the Codejam tasks, one usually have to read a whitespace-limited sequence of numbers. When I solve the problems in Lisp, all I need to do is to call READ. A flextible IO probrary that makes these kinds of this simple would be nice.
I could (and indeed have considered to) write such functions in APL, but this causes a second problem:
Error handling in GNU APL is very bad. In particular, there is nothing similar to UNWIND-PROTECT (or try/finally in Java). There is no way to safely write code that opens a file, works on it and then closes it. If an error occurs, there is no way to ensure that the filehandle is closed. When I developed my solution to the Codejam problem, I ended up leaking a lot of file handles.
Does anyone know how Dyalog and other vendors handle this? Do they have a full exception system?
Regards,
Elias
[Prev in Thread] | Current Thread | [Next in Thread] |