« first day (2348 days earlier)      last day (312 days later) » 

8:07 AM
@Adám is this one of the many challenges that would benefit from a conditional execution operator (something like C←{⍺⍺⍣⍺⊢⍵}) or am I doing it wrong?
 
8:20 AM
anyways, because I'll be away for the whole week, here are my very early entries:
{⌽⍣(>/⍵)(⊢,(⍳|-/⍵)+⊢)⌊/⍵}
>/{⌽⍣⍺⊢⍵}(⌊/,⌊/+(⍳|⍤(-/)))
 
 
1 hour later…
9:22 AM
Hello, I'm starting my APL journey, so let me apologize in advance if I'm being obtuse. Also, I'm not sure I can post, as per the instructions, but let me give it a try
My first APL program (as one does) calculates the factorial of a number. Leaving aside !, this is easy enough:

fact ← {×/1 + ⍳⍵}

The problem comes if I try to then take the factorial of various numbers in a vector, as then ⍳ generates multidimensional indices, instead of "mapping" into the vector
I mean, ⍳5 6 is not ⍳5 and then ⍳6. Is there any way to force functions to apply element-wise? Would that be the right way to do this? Thanks!
 
9:40 AM
@Schiphol what APL variant are you using here that has index origin 0 by default?
anyways you're looking for the Each operator (the diaeresis)
 
9:58 AM
Oh, right, I just changed the origin, following the Learning APL book, then forgot :)

Thanks, @RubenVerg! I need to add Each twice, right?

fact←{×/¨1+⍳¨⍵}
 
@Schiphol In addition to the each operator, you might want to look at the scan operator. You can then compute all of the factorials up to the maximum needed and index the result.
Scan operator is \
 
10:10 AM
@PaulMansour, thanks, so, something like this?

fact ← {(×\1 +⍳⌈/⍵)[⍵ - 1]}
Parentheses are probably frowned upon? There should be a way to make the calculation flow without them?
 
10:34 AM
@Schiphol Yes, exactly. You will probably need parens no matter how you do it, especially as you are using index origin 0. You can look at squad indexing ⌷ to get the indices on the left, but you will still need parens I think.
I don't frown on one level of parentheses. I do on nested parentheses.
 
10:57 AM
Thanks! This is nicer than my first try :)
 
11:43 AM
@Schiphol there is a way to only use one
remember, you don't need to use operators just on primitives
also your way creates a big nested array which is generally considered not great for performance
@Schiphol note that this fails on matrices! I would write something like {(⊂⍵)⌷×⍀⍳⌈⌿,⍵}
or well obviously ⊂⌷×⍀∘⍳∘(⌈⌿)∘, but you don't know these yet
 
12:22 PM
@RubenVerg thanks, I'll get back to factorials in a minute!

Right now I had moved on to Fibonacci, and I had two questions about my solution:

fib2 ← {
⍵≤1: ⍵
1 ↓ ({1 ↓ (⍵,+/⍵)} ⍣ (⍵ - 1)) 0 1
}

First of all, nested parens, which @PaulMansour said are to be avodied. Can I do something like the above without them?

Secondly, I'm using ⍵ in two different scopes, which is also perhaps bad style?
 
12:37 PM
@RubenVerg Here you could just as well do ⍳⌈/,⍵, right? Once you ravel, reduce and reduce first do the same thing. This is a matter of preference, I take it?
 
 
3 hours later…
3:49 PM
yes they do the same thing, I just tend to use the first versions as habit
@Schiphol I would use Binet's formula here :) but looking at your solution a big issue I see is that you return a 1-element vector for >1 arguments
(instead of a scalar)
 
4:25 PM
Right! Should be

]dinput
fib2 ← {
⍵≤1: ⍵
2 ⊃ ({1 ↓ (⍵,+/⍵)} ⍣ (⍵ - 1)) 0 1
}
(and yes re: Binet's formula! Just trying to learn how basic stuff works :)
 
4:43 PM
I think I would suggest a scan instead of a reduction
just as an example, here is one possibility: {{⌽+\⍵}⍣⍵⊢0 1}
 

« first day (2348 days earlier)      last day (312 days later) »