Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: APL function

227 views
Skip to first unread message
Message has been deleted

el...@ellismorgan.co.uk

unread,
Aug 27, 2012, 5:11:25 AM8/27/12
to
On Sunday, 26 August 2012 00:00:12 UTC+1, APL Learner wrote:
> Hi.
>
> Do you have any hints for how to create a function that can read whatever number we put.
>
>
>
> like we put a number such as : 10011.112 and the function has to give the number as : TEN THOUSAND AND ELEVEN POINT HUNDRED AND TWELVE.
>
>


>
> Do i have to assign each number like... 1←One 2←Two.... ?
>
>
>
> Well if you can give me some help it would be much appreciatted.
>
>
>
> Thanks !!!

I would write a function like "shaaquilC" to do the first hundred numbers ...

⎕io
0
(100|186)⊃shaaquilC
EIGHTYSIX
∇shaaquilC[⎕]∇
[0] w←shaaquilC;a;b;c;⎕IO
[1] ⍝ words for integers 0 to 99
[2] ⎕IO←0
[3] a←'NOUGHT' 'ONE' 'TWO' 'THREE' 'FOUR' 'FIVE' 'SIX' 'SEVEN' 'EIGHT' 'NINE'
[4] b←a,¨⊂'TEEN'
[5] ((⊂0 1 2 3 5 8)⌷b)←'TEN' 'ELEVEN' 'TWELVE' 'THIRTEEN' 'FIFTEEN' 'EIGHTEEN'
[6] c←(2↓a),¨⊂'TY'
[7] ((⊂0 1 2 6)⌷c)←'TWENTY' 'THIRTY' 'FORTY' 'EIGHTY'
[8] w←a,b,,c,c∘.,1↓a
[9] →0
[10]
[11] ⍝ 'EIGHTYSIX'≡ (100|186)⊃ shaaquilC

I am using DyalogAPL version 12.1

then I would think about these results for the main function ...

⍝ 'A' 'HUNDRED' 'AND' 'TWENTYSIX' ≡ shaaquill 126
⍝ 'A HUNDRED AND TWENTYSIX' ≡ ⍕shaaquill 126
⍝ 'EIGHTYSIX' ≡ ⍕shaaquil 86
⍝ 'MINUS ONE'≡ ⍕shaaquill ¯1
⍝ 'NOUGHT POINT SIX'≡ ⍕shaaquill 0.6
⍝ 'NOUGHT POINT THREE HUNDRED AND THIRTYTHREE' ≡ ⍕3 shaaquill ÷3

⍝ 'ONE' ≡ ⍕shaaquil 1
⍝ 'TEN' ≡ ⍕shaaquil 10
⍝ 'A HUNDRED' ≡ ⍕shaaquil 100
⍝ 'A THOUSAND' ≡ ⍕shaaquil 1000
⍝ 'A MILLION' ≡ ⍕shaaquil 1000000
⍝ 'A BILLION' ≡ ⍕shaaquil 1000000000
⍝ 'A TRILLION' ≡ ⍕shaaquil 1000000000000
⍝ 567 890 987 654 3 2 1≡1000000 1000 1000 1000 10 10 10 ⊤ 567890987654321
⍝ 567 890 987 654 3 21≡1000000 1000 1000 1000 10 100 ⊤ 567890987654321

⍝ 'A THOUSAND AND TWENTYTHREE' ≡ shaaquill 1023
⍝ 'ONE THOUSAND, ONE HUNDRED AND TWENTYTHREE' ≡ shaaquill 1123

⍝ 'ZERO' ≡ ⍕shaaquil 0
⍝ '1125899906842624.___ is too large' ≡ ⍕3 shaaquil 2*50
⍝ '10000000000000_ is too large' ≡ ⍕0 shaaquil 1E16
⍝ 'ZERO' ≡ ⍕ 3 shaaquil ÷2*50

Kerry Liles

unread,
Aug 27, 2012, 10:37:10 AM8/27/12
to
On 8/25/2012 7:00 PM, shaa...@live.co.uk wrote:
> Hi.
> Do you have any hints for how to create a function that can read whatever number we put.
>
> like we put a number such as : 10011.112 and the function has to give the number as : TEN THOUSAND AND ELEVEN POINT HUNDRED AND TWELVE.
>
> Do i have to assign each number like... 1←One 2←Two.... ?
>
> Well if you can give me some help it would be much appreciatted.
>
> Thanks !!!
>

http://lmgtfy.com/?q=convert+number+to+words

Rex Swain

unread,
Aug 27, 2012, 10:54:49 AM8/27/12
to
Here is some ancient code from before the days of nested arrays. You
would call this two times, once for the whole part and once for the
decimal part "POINT ...".

∇ R←WORDS X;A;B;C;D;H;O;P;Q;S;T;⎕CT;⎕IO
[1] ⍝ TRANSLATE AN INTEGER INTO WORDS
[2] ⍝ REQUIRES: -
[3] ⍝ REX SWAIN 11 MAY 1976; ENHANCED SLIGHTLY 30 DECEMBER 1981
[4] ⎕CT←0 ⋄ ⎕IO←1
[5] S←+/⎕SI[;⍳6]^.='WORDS[' ⍝ RECURSION DEPTH
[6] →(0≠X)⍴a ⋄ R←(1=S)/'ZERO' ⋄ →0
[7] a:A← 10 6 ⍴'ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT
NINE '
[8] B←'TEN ELEVEN TWELVE THIRTEEN FOURTEEN FIFTEEN '
[9] B← 10 10 ⍴B,'SIXTEEN SEVENTEEN EIGHTEEN NINETEEN '
[10] C←' TWENTY THIRTY FORTY FIFTY SIXTY '
[11] C← 10 8 ⍴C,'SEVENTY EIGHTY NINETY '
[12] D← 5 9 ⍴' THOUSAND MILLION BILLION TRILLION '
[13] P←1+Q← 10 10 10 ⊤X ⍝ LAST THREE DIGITS
[14] H←(×Q[1])/A[P[1];],'HUNDRED ' ⍝ HUNDREDS
[15] T←(2≤Q[2])/C[P[2];] ⍝ TENS
[16] O←((1=Q[2])/B[P[3];]),(1 0 ^.≠1↓Q)/A[P[3];] ⍝ TEENS OR ONES
[17] R←(WORDS⌊X÷1000),H,T,O,(0≠1000∣X)/D[S;] ⍝ RECURSE AND ASSEMBLE
[18] →(1≠S)⍴0 ⋄ B←R≠' ' ⋄ R←(B∨1⌽B><\B)/R ⍝ DELETE EXTRANEOUS BLANKS


My colleague Bob Decloss wrote another version that handles a whole
array of numbers without recursion. Nice!

∇ R←WORDS2 N;A;B;C;D;a;b;c;d
[1] ⍝ CONVERTS INTEGER ARRAY INTO WORDS
[2] ⍝ REQUIRES: -
[3] ⍝ BOB DECLOSS, CIRCA 1976
[4] a← 10 6 ⍴'////////ONE //TWO THREE /FOUR /FIVE //SIX SEVEN EIGHT
/NINE '
[5] b←'///////////////////////TWENTY ///THIRTY ////FORTY ////FIFTY ////'
[6] b←b,'SIXTY //SEVENTY ///EIGHTY ///NINETY //////TEN ///ELEVEN ///'
[7] b←b,'TWELVE /THIRTEEN /FOURTEEN //FIFTEEN //SIXTEEN SEVENTEEN /'
[8] b← 20 10 ⍴b,'EIGHTEEN /NINETEEN '
[9] c←'////////////////ONE HUNDRED //TWO HUNDRED THREE HUNDRED /'
[10] c←c,'FOUR HUNDRED /FIVE HUNDRED //SIX HUNDRED SEVEN HUNDRED '
[11] c← 10 14 ⍴c,'EIGHT HUNDRED /NINE HUNDRED '
[12] d← 4 11 ⍴'/////////////THOUSAND ///MILLION ///BILLION '
[13] C←3×D←⌈(⍴⍕⌈/N←,N)÷3 ⋄ B←''⍴⍴A←((D×⍴N),3)⍴⍉(C⍴10)⊤N
[14] C←(A[;2]=1)/⍳B ⋄ A[C;2]←A[C;3]+10×A[C;2]
[15] A[C;3]←0 ⋄ A←A+1
[16] R←,c[A[;1];],b[A[;2];],a[A[;3];],d[1⌈(∨/1<A)×⌽B⍴⍳D;]
[17] R[(R='/')/⍳⍴R]←⎕TCDEL ⋄ R←((⍴N),D×41)⍴R


In those days, the ⎕TCDEL character did not print at all. Many tricks
were used before nested arrays!

Roger Hui

unread,
Aug 29, 2012, 2:00:24 PM8/29/12
to
Not APL, but shows how this is a very amusing problem:
http://www.jsoftware.com/jwiki/Essays/Number%20in%20Words . Herewith,
solutions in English (UK), English (USA), and Chinese:

2^53x
9007199254740992

uk 2^53x
nine quadrillion, seven trillion, one hundred and ninety-nine billion,
two hundred and fifty-four million, seven hundred and forty thousand,
nine hundred and ninety-two
us 2^53x
nine quadrillion, seven trillion, one hundred ninety-nine billion, two
hundred fifty-four million, seven hundred forty thousand, nine hundred
ninety-two
zh 2^53x
九千零七兆一千九百九十二億五千四百七十四萬零九百九十二

simond...@googlemail.com

unread,
Aug 30, 2012, 6:49:12 AM8/30/12
to
For another APL solution take a look at the following page on the APL Wiki:

http://aplwiki.com/SpellNumbers/Task?highlight=%28\bCategoryPuzzles\b%29

Phil Last

unread,
Aug 30, 2012, 1:46:09 PM8/30/12
to
Of course, being good Europeans we Brits should abandon Harold Wilson's precipitate action and go back to the much more logical system the rest of Europe uses:

Either:

nine thousand and seven billion, one hundred and ninety-nine thousand, two hundred and fifty-four million, seven hundred and forty thousand, nine hundred and ninety-two.

or

nine billiard, seven billion, one hundred and ninety-nine milliard, two hundred and fifty-four million, seven hundred and forty thousand, nine hundred and ninety-two.

Phil Last

unread,
Aug 30, 2012, 2:27:35 PM8/30/12
to
And while wer'e at it we should probably drop forty, sixty and eighty and revert to two-score, three-score and four-score respectively.

(Timed at five-and-twenty past six GMT.)
Message has been deleted
Message has been deleted

Roger Hui

unread,
Sep 4, 2012, 12:43:21 AM9/4/12
to
The statement "the function [has] to read the number that the user
type" has some wriggle room and you should exploit that to write the
function in a good style. For example, define:

GlobalTable ← shaaquilC

z←NumberInWords x;⎕io
⎕io←0
z←x⊃GlobalTable

And to use it you can do
NumberInWords 86
NumberInWords 13
etc.

It can reasonably be argued that in the above the function _is_
reading numbers that the user typed. Writing a function that takes an
argument and returns a result is a good style because then you can
apply it to arrays of numbers, compose it with other things, etc. For
example:
NumberInWords¨ 86 13 17

If it turns out that you have no wriggle room, then ⎕ or ⍞ can be used
to "read the number that the user types" and then you apply
NumberInWords to that. But it'd be perverse and archaic to pose such
a problem.

If it's undesirable to have a global table, then what you can do is to
rewrite shaaquilC to take an argument x (say); keep the current lines;
and at the end add a new line to do x⊃w and assign it to the explicit
result.

Finally, it seems like a hearty "Welcome to APL" is in order.




On Sep 3, 3:17 am, APL Learner <shaaq...@live.co.uk> wrote:
> Also if i want to test the function... do i do.. : 'Twenty' shaaquilC or '20' shaaquilC.
> because the function as to read the number that the user type,.
> Thanks for your help

el...@ellismorgan.co.uk

unread,
Sep 4, 2012, 9:54:36 AM9/4/12
to
On Sunday, 26 August 2012 00:00:12 UTC+1, APL Learner wrote:
> Hi.
>
> Do you have any hints for how to create a function that can read whatever number we put.
>
>
>
> like we put a number such as : 10011.112 and the function has to give the number as : TEN THOUSAND AND ELEVEN POINT HUNDRED AND TWELVE.
>
>
>
> Do i have to assign each number like... 1←One 2←Two.... ?
>
>
>
> Well if you can give me some help it would be much appreciatted.
>
>
>
> Thanks !!!

hello again APL Learner

Like Roger Hui I would like to welcome you to APL.

My function was intended as an example of how I would code a small part of the your requirements (handling the numbers from 0 to 99). I did not go further because I was unclear whether you are on a coding exercise or are looking for a solution that you can just lift and use.

Also I was unclear how you want to handle the larger numbers (to start with is a billion a thousand million or a million million?). As Roger and Phil Last have pointed out it is done differently in Europe, the UK and the US.

I have nothing to add to Roger's comments on how to build a function round the "shaaquilC" example I posted.

APL Learner

unread,
Sep 5, 2012, 4:48:00 AM9/5/12
to
Ok I think i got a solution for my problem ,thanks for your help :)
I started learning APL 1 month ago. Its hard, because of the syntax.. So many symbols... hopefuly i can learn this as soon as possible.

APL Learner

unread,
Sep 5, 2012, 4:52:38 AM9/5/12
to
On Tuesday, September 4, 2012 2:54:36 PM UTC+1, (unknown) wrote:
Hi,
May I ask have you got any hints for me , to how to learn APL as quick as possible ?
Thanks

Roger Hui

unread,
Sep 5, 2012, 10:20:29 AM9/5/12
to
> May I ask have you got any hints for me , to how to learn APL as quick as possible ?

Ken Iverson was a good writer. I once asked him the secret of writing
well. His reply? "First, write 500 papers." http://keiapl.org/rhui/remember.htm#500

I think you already are on the path of learning APL quickly: Write
lots of APL; read lots of APL. Iverson's _The Inductive Method of
Introducing APL_ http://www.jsoftware.com/papers/InductiveMethod.htm
and _Programming Style in APL_ http://www.jsoftware.com/papers/APLStyle.htm
may be helpful.

Rav

unread,
Sep 5, 2012, 10:30:04 AM9/5/12
to
One suggestion I would make is to just try lots of things. Unlike many
languages (even some "modern" ones), APL is very forgiving to the user,
both when working in immediate execution mode and when executing
user-defined functions. One of the first things I was told when I
started learning APL in 1970 was that you can't "get hurt" trying
things, and it's true today too. And it's fun to try things!

As far as "so many symbols," it may seem daunting at first, but there
are fewer such symbols than there are reserved words in many other
languages. And, sometimes the actual symbols themselves start making
sense in the way they look, literally, such as ⌽ which of course is
rotate. Doesn't it look like it's rotating? And grade up ⍋ points
up, grade down ⍒ points down, etc. That doesn't mean there aren't
nuances, but you will pick them up. And APL applies its rules strictly:
For example, reduction works the same no matter which operator it's
being applied to, no matter how many dimensions the array being operated
on has.

I hope that helps a little. /Rav
Message has been deleted

APL Learner

unread,
Sep 6, 2012, 6:22:56 AM9/6/12
to
Hi I have been trying to do lots of stuff such as a Calculator, I also started doing Tic Tac Toe but I got stuck as I dont know how to pick each cell and change for ( circles or crosses).. (if you know what i mean)...

Rav

unread,
Sep 6, 2012, 9:38:20 AM9/6/12
to
Not sure what you mean. Do you mean as in assigning X or O to certain
squares, as in:

TTT←3 3⍴' '
TTT[1;1]←'X' ⍝ Original method of indexing which still works fine.
TTT[2;2]←'O'
TTT[2;1]←'X'
TTT[⊂3 1]←'O' ⍝ Newer method of indexing (Nested Arrays).
TTT
X
XO
O

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Kerry Liles

unread,
Sep 6, 2012, 10:47:16 AM9/6/12
to
On 9/6/2012 10:27 AM, APL Learner wrote:
> On Tuesday, September 4, 2012 2:54:36 PM UTC+1, (unknown) wrote:
> Hello, sorry once again...
> I'm creating a form.
> I have taken the bar on the top ('SysMenu' 0)...
> However now I want to create a button which will CLOSE the form...
> I have tried .. 'F.quit'⎕WC'Button' 'Quit'(350 590)(25 80)('Event' 'Select' 'Close' 'F')('FCol' 6 102 186)('Font' 'Arial' 26)
> 'Close' is a function :
> F Close Msg
> ⎕EX F
> And it doesnt work , when I click the button Quit it just doesnt do anything.
> I dont know what to do now...
> Have you got any hints ?
> Also im running Dyalog 13.0 ...
> Thank you.
>

Please do not post the same thing over and over (even in the same thread!)

Rav

unread,
Sep 6, 2012, 1:01:55 PM9/6/12
to
On 9/6/2012 10:26 AM, APL Learner wrote:
> Hello, sorry once again...
> I'm creating a form.
> I have taken the bar on the top ('SysMenu' 0)...
> However now I want to create a button which will CLOSE the form...
> I have tried .. 'F.quit'⎕WC'Button' 'Quit'(350 590)(25 80)('Event' 'Select' 'Close' 'F')('FCol' 6 102 186)('Font' 'Arial' 26)
> 'Close' is a function :
> F Close Msg
> ⎕EX F
> And it doesnt work , when I click the button Quit it just doesnt do anything.
> I dont know what to do now...
> Have you got any hints ?
> Also im running Dyalog 13.0 ...
> Thank you.
>

I'm not a Dyalog APL user. However, I used APL2000's APL+Win system for
many years, and what you posted seems to have a similarity to the way
their ⎕WI system function works. IF I understand correctly, and I'm
only guessing here, when the button Event is Select, you want to execute
the Close function with a left argument of 'Select' and a right argument
of 'F' ? Actually, given the syntax of your Close function, it looks to
me like the arguments are reversed ('Select' looks like the message you
want to display, 'F' looks like the name of the form you want to
delete). At any rate, I don't know Dyalog's syntax in this case, but in
APL+Win, the argument to be executed would look something like this:

'"Select" Close "F"'

There is ONE string to execute, bounded by the SINGLE quotes, whereas
the way you tried to do it was with three SEPARATE strings. Even though
I don't know Dyalog syntax, I would suspect that three separate strings
wouldn't be the way to code a handler. Info: There happen to be two
strings referenced within that string, which are bounded by DOUBLE
quotes. Using single and double quotes is simply an easy way to embed
strings within strings. In older APL systems which didn't support both
forms of quotes, one would have written:

'''Select'' Close ''F'''

So, try using a single string like that as your handler.

Another thing: Again, not actually knowing Dyalog syntax, I wouldn't
think that ⎕EX would be the way to close a form. I only know of ⎕EX
being able to erase functions and variables. But perhaps in Dyalog ⎕EX
supports that. In APL+Win, for example, one would close the form like this:

'F' ⎕WI 'Close'

/Rav

Rav

unread,
Sep 6, 2012, 1:04:00 PM9/6/12
to
On 9/6/2012 10:27 AM, APL Learner wrote:
> On Tuesday, September 4, 2012 2:54:36 PM UTC+1, (unknown) wrote:
> Hello, sorry once again...
> I'm creating a form.
> I have taken the bar on the top ('SysMenu' 0)...
> However now I want to create a button which will CLOSE the form...
> I have tried .. 'F.quit'⎕WC'Button' 'Quit'(350 590)(25 80)('Event' 'Select' 'Close' 'F')('FCol' 6 102 186)('Font' 'Arial' 26)
> 'Close' is a function :
> F Close Msg
> ⎕EX F
> And it doesnt work , when I click the button Quit it just doesnt do anything.
> I dont know what to do now...
> Have you got any hints ?
> Also im running Dyalog 13.0 ...
> Thank you.
>

See my reply in your other (duplicate) post in this same thread.
Message has been deleted
0 new messages