myZip and myZipWith functions ala haskell
Moderator: MSandro
myZip and myZipWith functions ala haskell
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].)
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.
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.
Re: myZip and myZipWith functions ala haskell
Nice!
The "myZip" function could be simplified. It doesn't need to create and call a function; it could just do the job directly.
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
thanks for the advice - it's much simpler now.
here's the updated source:
Re: myZip and myZipWith functions ala haskell
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.)
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
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.
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
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!)
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!)
- Attachments
-
- my_luhn_func.gpp
- (7 KiB) Downloaded 1390 times
Re: myZip and myZipWith functions ala haskell
1st stage is to just put all your functions in its own class
Re: myZip and myZipWith functions ala haskell
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 :)
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 :)
Re: myZip and myZipWith functions ala haskell
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.