reset password
Author Message
avarg116
Posts: 8
Posted 11:35 Mar 04, 2019 |

Hello All,

Here is the link to the Github Repo for the solutions presented today:

https://github.com/austinv211/Functional-Libraries

Thought I would post for any of those interested in the way I implemented the Haskell solution to problem number 5 in Python.

Thanks,

Austin Vargason

rabbott
Posts: 1649
Posted 15:37 Mar 04, 2019 |

Thanks, and nice work today. Do you have a Haskell solution to Problem 4? I didn't see it in https://github.com/austinv211/Functional-Libraries.

Last edited by rabbott at 15:37 Mar 04, 2019.
kverma
Posts: 6
Posted 17:00 Mar 04, 2019 |

Thanks Austin !! 

avarg116
Posts: 8
Posted 18:50 Mar 04, 2019 |

Hi,

I updated the Github link to include the solution to problem 4 in Haskell.

https://github.com/austinv211/Functional-Libraries

Thanks,

Austin Vargason

rabbott
Posts: 1649
Posted 20:09 Mar 04, 2019 |

Unless I'm reading it wrong, it looks to me like there are a couple of problems. Here's the code.

myLuhn :: Int -> Bool
myLuhn n =
    let fn = sum . \c -> divMod (2 * c) 10
        checksum = sum [ if f == 1 then read [c] else fn (read [c]) | (f, c) <- zip (cycle [0, 1]) (reverse (show n))]
    in (mod checksum 10) == 0

testCC :: [Bool]
testCC = map myLuhn [1234567890123456, 1234567890123452]

a). divMod returns a tuple, but  sum doesn't work properly on tuples.

b) checksum looks like it doubles every odd digit rather than every even digit.

testCC does return the correct answer for the two test cases. But myLuhn 91 returns False even though  myLuhn 91 should return True. 

91 => 9 1 => (18) 1 => 9 1 => 10

 

 

 

Last edited by rabbott at 20:15 Mar 04, 2019.
jungsoolim
Posts: 38
Posted 21:16 Mar 04, 2019 |

Jay came up with this version. I think this is pretty good and I would like to share it with you.

toDigit :: Char -> Int   
toDigit = read . (:[]) 

uncurry' :: (t2 -> t1 -> t) -> (t2, t1) -> t
uncurry' f = \(x, y) -> f x y

fn :: Int -> Int
fn = uncurry' (+) . (`divMod` 10) . (*) 2

myLuhn' :: Int -> Bool
myLuhn' n = 0 == mod checkSum 10
            where checkSum = sum [f x | (f,x) <- zip (cycle [id, fn]) $ (reverse . map toDigit . show) n]

testCC :: [Bool]
testCC = map myLuhn' [49927398716, 49927398717, 1234567812345678, 1234567812345670, 91]

 

{-

In cycle [id, fn] , 'id' is a id function which does not do anything. It will just return the original value.

-}

{- Or make it Point - free as this

myLuhn :: Int -> Bool
myLuhn = (0 == ) . (`mod` 10 ) . (\x -> sum [f x | (f,x) <- zip (cycle [id, fn]) $ (reverse . map toDigit . show) x])


testCC :: [Bool]
testCC = map myLuhn [49927398716, 49927398717, 1234567812345678, 1234567812345670, 91]

-}

Last edited by jungsoolim at 21:34 Mar 04, 2019.
rabbott
Posts: 1649
Posted 21:33 Mar 04, 2019 |

Looks good! Thanks for posting it.

avarg116
Posts: 8
Posted 12:08 Mar 05, 2019 |

Thanks Soo and Jay!

Didn't spend much time on problem4 due to working on the other problems.

I updated the code to fix the issues in the way I implemented it.