Post Reply 
Syntax Problems with map() and apply()
05-24-2016, 08:07 PM (This post was last modified: 05-24-2016 11:57 PM by mark4flies.)
Post: #4
RE: Syntax Problems with map() and apply()
Thanks so much for your help. It has been some time since I programmed a calculator. I used the HP-41 extensively through chemistry graduate school and beyond, and later the HP-42 and HP-48 through HP-50 as well. I am getting back to it now with the HP Prime, but it is a very different machine in so many ways.

I was using Mark Power's blog to help. One of his early posts about the Prime involved programming a card game. His approach to shuffling used explicit loops. I am used to other computer languages that support data structures such as lists and vectors with functions like apply() and map() so I thought I might apply one of my usual techniques for randomization. We try to 'vectorize' operations for the sake of concise code and algorithm performance. In the case of the card deck, you generate a vector of 52 uniformly random variables, sort them into another vector, then find their position in the original, random vector. The positions (index) becomes a random index. You essentially transfer the randomness to the positions. This way is usually much simpler and cheaper than other methods. I thought I would try it out.

Here is my program, with your help, just for this aspect of the game:

#cas
f(l1,l2) := apply('x->POS(l1,x),l2);
#end

EXPORT RANDOMIZE()
BEGIN
L1 := MAKELIST(RANDOM(),X,1,52);
L2 := SORT(L1);
RETURN f(L1,L2);
END;

The RANDOMIZE program returned these timings using Eddie's TIMESHUFFLE() program to time the shuffling of a 52-card deck:

HP Prime:
0.073s
iPad Pro app:
0.011s
HP Prime Virtual Calculator (Windows):
0.00389
HP Prime Virtual Calculator (Macintosh):
0.00384s

I am surprised that the vectorization approach performed so poorly versus the explicit looping approach, but there you have it. For an 'apples to apples' comparison, it took only 0.02s for Mark's original solution. It took mine more than three times as long!

So I have a lot to learn about this machine and programming it, but many thanks already for getting me started down the right path!
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Syntax Problems with map() and apply() - mark4flies - 05-24-2016 08:07 PM



User(s) browsing this thread: 1 Guest(s)