myZip and myZipWith functions ala haskell

Extending or modifying the GP system itself (advanced!)

Moderator: MSandro

Post Reply
manyone
Posts: 11
Joined: Nov 5th, '17, 07:56

myZip and myZipWith functions ala haskell

Post by manyone » Dec 12th, '17, 07:33

when i learned about GP, i immediately tried to use it to encode the luhn algorithm that checks for good credit card numbers- but soon learned that GP did not have a zipWith function! so i wrote my own version. ( in haskell, zipWith (*) [2,3,4] [5,4,6] would return a list of [10,12,24].)
Image

in my version, i wrote the myZip command first which zips the 2 input lists into a list of pairs, then map the multiply function over the elements of that list. i wanted to retain the ability to supply any 2-argument function at the time of calling myZipWith.

(i probably over coded it - i couldn't figure out a way of passing a function as a parameter to another function other than shown here).

i'm also attaching the short program for anyone interested.
myZipWith.gpp
(6.32 KiB) Downloaded 54 times
Attachments
zipwith copy.jpg

JohnM
Posts: 359
Joined: Sep 11th, '15, 14:42

Re: myZip and myZipWith functions ala haskell

Post by JohnM » Dec 20th, '17, 10:25

Nice!

The "myZip" function could be simplified. It doesn't need to create and call a function; it could just do the job directly.

manyone
Posts: 11
Joined: Nov 5th, '17, 07:56

Re: myZip and myZipWith functions ala haskell

Post by manyone » Dec 21st, '17, 04:54

thanks for the advice - it's much simpler now.
myzipwith copy.jpg
here's the updated source:
myZipWith.gpp
(5.98 KiB) Downloaded 77 times

JohnM
Posts: 359
Joined: Sep 11th, '15, 14:42

Re: myZip and myZipWith functions ala haskell

Post by JohnM » Dec 29th, '17, 15:42

Nice! Short and elegant.

Have you used it to implement the Luhn algorithm for checking credit card numbers? (I did not know about the Luhn algorithm before; I had to look it up.)

manyone
Posts: 11
Joined: Nov 5th, '17, 07:56

Re: myZip and myZipWith functions ala haskell

Post by manyone » Dec 30th, '17, 03:41

yes, i did, using myZip, myZipWidth and a couple of helper functions. and it validated 1234567812345670 and 49927398716 (fake numbers, of course) as good credit card numbers. i hesitate to upload the program because the problem is a perfect exercise for programmers learning a new language.
taking 49927398716 as example. the steps would be
1. reverse the digits: 61789372994
2. sum the odd digits: 6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
3. apply times_2 to the even digits: 2, 16, 6, 4, 18 and sum the digits of the products: 2, 7, 6, 4, 9
4. sum the even digit partial sums: 2 + 7 + 6 + 4 + 9 = 28 = s2
5. s1 + s2 = 70, which ends in zero, therefore 49927398716 passes the Luhn test.
the program, properly coded using map, zip, zipwith etc, can show the dramatic differences between imperative and functional programming.

manyone
Posts: 11
Joined: Nov 5th, '17, 07:56

Re: myZip and myZipWith functions ala haskell

Post by manyone » Jan 3rd, '18, 06:40

i've decided to upload my implementation of the luhn algorithm, if anyone's interested. Comments are welcome.
i realize that there are many things about GP i still don't know. For starters, how do i put my often-used functions like myZip, myZipWith, sum, etc into some form of library so i dont have to recode them next time i need them but simply drag them into the workspace? (and i haven't even tried understanding classes and methods yet!)
my_luhn_func copy.jpg
Attachments
my_luhn_func.gpp
(7 KiB) Downloaded 48 times

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: myZip and myZipWith functions ala haskell

Post by SimpleSi » Jan 3rd, '18, 16:38

1st stage is to just put all your functions in its own class
swmod_luhn_func.gpp
(7.49 KiB) Downloaded 85 times

SimpleSi
Posts: 326
Joined: Jul 2nd, '17, 13:47

Re: myZip and myZipWith functions ala haskell

Post by SimpleSi » Jan 3rd, '18, 16:52

Then by following

https://gpblocks.org/forum/viewtopic.ph ... bb7cd#p783

you can create a new category called Luhn and save the file (Just as a normal GP file but I usually add ext in the filename for ease of use)

Then the trick is to right click on stage and select import extension

Voila!

You will have a new category called Luhn that users can just use :)

Its one of the most brilliant features of GP - we can can write extensions in GP and add then distrute them to others as well

Hopefully, in the distant future, we will have the ability to do an online search/download for extensions :)
0.png
swmod_ext_luhn_func.gpp
(7.52 KiB) Downloaded 51 times

manyone
Posts: 11
Joined: Nov 5th, '17, 07:56

Re: myZip and myZipWith functions ala haskell

Post by manyone » Jan 4th, '18, 07:51

worked like a charm! thank you! this block extension mechanism is totally awesome! we can share code, programs and whole libraries and it's so easy.

Post Reply