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
 mark4flies Member Posts: 115 Joined: Jan 2015
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
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!
 « Next Oldest | Next Newest »

 Messages In This Thread Syntax Problems with map() and apply() - mark4flies - 05-23-2016, 12:01 PM RE: Syntax Problems with map() and apply() - mfleming - 05-23-2016, 02:02 PM RE: Syntax Problems with map() and apply() - DrD - 05-23-2016, 02:22 PM RE: Syntax Problems with map() and apply() - mark4flies - 05-24-2016 08:07 PM RE: Syntax Problems with map() and apply() - mfleming - 05-26-2016, 06:13 AM RE: Syntax Problems with map() and apply() - parisse - 05-26-2016, 12:30 PM RE: Syntax Problems with map() and apply() - Joe Horn - 05-27-2016, 04:21 AM RE: Syntax Problems with map() and apply() - salvomic - 05-27-2016, 08:01 AM RE: Syntax Problems with map() and apply() - DrD - 05-27-2016, 10:23 AM RE: Syntax Problems with map() and apply() - mark4flies - 05-27-2016, 01:39 PM RE: Syntax Problems with map() and apply() - mark4flies - 05-27-2016, 02:07 PM RE: Syntax Problems with map() and apply() - DrD - 05-27-2016, 03:07 PM RE: Syntax Problems with map() and apply() - parisse - 05-28-2016, 07:16 AM RE: Syntax Problems with map() and apply() - mark4flies - 05-28-2016, 08:45 PM RE: Syntax Problems with map() and apply() - mark4flies - 05-28-2016, 08:46 PM RE: Syntax Problems with map() and apply() - parisse - 05-29-2016, 06:00 AM RE: Syntax Problems with map() and apply() - mark4flies - 05-28-2016, 08:49 PM RE: Syntax Problems with map() and apply() - DrD - 05-28-2016, 09:32 PM

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