4

So, I'm wondering/asking; Is it possible to do an If-Statement in APL? If so how?

Here's my code

    'Please enter a number to count to: ' 
 number ←⎕ 
 ⍳number

How do I get an if-statement to where if the user inputs a number over 100 it will print out "too high" and end; or if it's 100 or under then it will just continue?

Thanks!

MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Possible duplicate of [Conditional function in APL](http://stackoverflow.com/questions/15752895/conditional-function-in-apl) – Uwe Jun 15 '16 at 20:30

4 Answers4

8

In Dyalog APL you have this neat little thing called guards.

They can be used in dfns and evaluate code when a certain condition matches.

func ← {⍵>100 : 'too high' ⋄ 1 : 'number is ok'}
Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
ein mensch
  • 311
  • 2
  • 9
  • 1
    Why did someone down-vote this answer? The explanation is a little short, but it is a very modern and clean way of answering. You can try it here: http://tryapl.org/ – Thomas Baruchel Nov 03 '16 at 10:39
  • 2
    What is the significance of the `1 : ` part (in the false branch after `⋄`)? – P Varga Oct 17 '20 at 16:22
6

If your APL supports control structures then this should work:

∇ generateAll number
:If number>100
   ⎕←'Too high'
:else
   ⎕←⍳ number
:endif
∇

If it does NOT support control structures (like APL2) you will need to branch:

∇ generateAll number
 →(number>100)/error
 ⎕←⍳ number
 →0
error:
 ⎕←'Too high'
∇

You can also use tricks like execute but this is less readable.

user229044
  • 232,980
  • 40
  • 330
  • 338
danb
  • 61
  • 2
  • the code display is all messed up, there should be 6 lines in each program shown above: a header line and 5 lines of code... – danb Jun 19 '16 at 13:12
  • The second example (without control structures) should be correct for GNU APL. – Lobachevsky Jun 22 '16 at 10:19
  • I tried all of these but none of them seem to really work. The second one for this one; I enter a number(both over and under 100) and it just stops at '∇ generateAll number' :/ –  Jul 09 '16 at 21:21
  • I think you misunderstood something. `generateAll` is a function and needs to be entered/defined as such. (I can't comment on how to do this in GnuAPL). Then you can enter `generateAll 50`in the session to execute the function. – MBaas Jul 10 '16 at 06:43
3

A "classical" way of doing error handling* in APL2 is with the ⎕ES or ⎕EA. Your code would look something like this:

⎕ES(NUMBER>100)/'Too high'
⍳NUMBER

What happens here is that IF the parentheses evaluate to true, THEN the ⎕ES will halt the execution and echo the quoted string.

If you don't want your THEN to terminate, have a look at ⎕EA in some APL documentation.

Please note that I'm on APL2 in a GreenOnBlack environment, so there are likely more neat ways of doing this in a more modern dialect like Dyalog.


*I know you're asking about conditionals and not error handling, but since you're example terminates execution, it might as well be error handling.

There is a crucial difference between this and what MBaas suggests: His solution will gracefully exit the current function which might return a value. Using ⎕ES or ⎕EA with terminate all execution.

mappo
  • 444
  • 2
  • 9
2

Depends on the dialect you're using. Some APL-Implementations support control-strucures, so you could write something like

:If number>100
   ⎕←'Too high'
   →0
:endif
⍳number

In "tradtional APL" you would probably do something like

⍎(number>100)/'⎕←''Too high'' ⋄ →0'
⍳number
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • None of those seem to be working :/ I enter a number over 500; "too high" isn't printed out and '⍳number' is ran. Basically what I'm trying to go for, is if it's too high, don't print out '⍳number' however if it's under 100, then do print it out. –  Jun 17 '16 at 21:08
  • Which APL are you using? – MBaas Jun 18 '16 at 06:05
  • Well I believe I'm using GNU APL 1.5; not sure if that helps. –  Jun 20 '16 at 22:32
  • Well, such details will help those answering in providing a suitable reply, as it elimates guesswork about the dialect. The answer of meagar (edited by DanB) considers the major possible pathways and should also apply for your case. Pls. have a look at that and, if it answers, also accept that, so that the question is closed. :-) – MBaas Jun 21 '16 at 02:51
  • Unfortunately none of those seem to work. The second one prints out the numbers if it's under 100. However if I input a number over 100, 'too high' prints out as expected; but then I see a Syntax Error+ at →0, then it still prints out the numbers. –  Jul 09 '16 at 21:51
  • The reply of DanB answers perfectly. Let's focus on getthing that to work for you. – MBaas Jul 10 '16 at 06:44