I was working on this function Happy
to practice control flow. Happy
tests if a number is a happy number. I was trying also to learn error handling. So, I tried to catch if a user input is anything other than an integer. Especially if the input is a string. For this I tried to incorporate the scalar test here But I could not test it on the editor because if I try to enter any letters without quotes I get of course a VALUE ERROR: Undefined name
.
Also, Mastering APL book warns about using Quad for data input.
What is the correct way to test if an input is a string? I assume that the user will enter letters without quotes.
(I'm using Dyalog APL 17.0)
(I took the code about how to split the number into its digits from here?)
Happy
'Enter a Number'
N←⎕
N2←N
:If N<0
N←|N
'You entered'N2
'Only positive numbers can be happy'
'We will check if'N'is happy'
N2←N
:EndIf
f←10⊥⍣¯1⊢
D←f N
N←+/D×D
⍝:If 0≢⊃0⍴⊂N
⍝:OrIf N≢⌊N
⍝ 'You entered: 'N'Enter an integer'
⍝ →0
⍝:EndIf
:If N=1
'***************'
'Yes'N2'is happy!'
'***************'
:Else
:While N≠1
N←+/D×D
D←f N
N←+/D×D
'N is:'N
:If N=4
'**********************************'
'Sorry!'N2'is not a happy number'
'**********************************'
:Leave
:EndIf
:If N=1
'**********************'
'Yes!'N2'is a happy number'
'**********************'
:EndIf
:EndWhile
:EndIf