Page 1 of 1

myZip and myZipWith functions ala haskell

Posted: Dec 12th, '17, 07:33
by manyone
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 678 times

Re: myZip and myZipWith functions ala haskell

Posted: Dec 20th, '17, 10:25
by JohnM
Nice!

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

Re: myZip and myZipWith functions ala haskell

Posted: Dec 21st, '17, 04:54
by manyone
thanks for the advice - it's much simpler now.
myzipwith copy.jpg
here's the updated source:
myZipWith.gpp
(5.98 KiB) Downloaded 730 times

Re: myZip and myZipWith functions ala haskell

Posted: Dec 29th, '17, 15:42
by JohnM
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.)

Re: myZip and myZipWith functions ala haskell

Posted: Dec 30th, '17, 03:41
by manyone
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.

Re: myZip and myZipWith functions ala haskell

Posted: Jan 3rd, '18, 06:40
by manyone
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

Re: myZip and myZipWith functions ala haskell

Posted: Jan 3rd, '18, 16:38
by SimpleSi
1st stage is to just put all your functions in its own class
swmod_luhn_func.gpp
(7.49 KiB) Downloaded 733 times

Re: myZip and myZipWith functions ala haskell

Posted: Jan 3rd, '18, 16:52
by SimpleSi
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 630 times

Re: myZip and myZipWith functions ala haskell

Posted: Jan 4th, '18, 07:51
by manyone
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.