DEV Community

Myoungjin Jeon
Myoungjin Jeon

Posted on • Updated on

Weekly Challenge #080 :: Haskell

While I solving PWC #080 with haskell, I realised that there are simpler way to solve with Raku as well.

I could solve task #1 with Data.Set in standard haskell module.
and I'm barely remember there is a Set and SetHash in Raku during reading Review of PWC #076

code below is my solution of task #1 in haskell

Task #1

...
{- tested with:
runhaskell ch-1.hs 5 -2 2 0
-}

smallestPostiveNumber ints = -- result "1" with empty list
  (head.dropWhile (isJust.((flip Set.lookupIndex) (Set.fromList ints)))) [1..]
...

and my previous raku solution can be even shorter :-]

[1..].first({@*ARGS.Set$_}).say

I believe that "∞" is actually 4bytes but "∌" is little shorter than "(cont).not"

and while I'm solving task #2 in haskell
I realise that I don't need to compare in both way..
need just one direction!

Task #2


countCandies []    = 0
countCandies ranks = defaultCandies + extraCandies
  where
    leftGroup = init ranks
    rightGroup = tail ranks
    defaultCandies = length ranks
    extraCandies =
      sum $ zipWith (\l r -> if l /= r then 1 else 0) leftGroup rightGroup

The original code I wrote is something like

    extraCandies1 =
      sum ( zipWith (\l r -> if l < r then 1 else 0) leftGroup rightGroup )
    extraCandies2 =
      sum ( zipWith (\l r -> if l > r then 1 else 0) leftGroup rightGroup )

and then.. I thought "what was that? isn't is just "/="? (/= is same as "!=" in Perl/Raku)

which means I could shorten the code in raku as well!

Original task #2 post

sub MAIN{(|@_,|(|@_.rotor(2=>-1).grep({[!=] $_}))).elems.say}

but my wife didn't understand why I'm smiling now.
p.s) I hope I could use "raku" in CodeBlock one day. (I'm using "perl" now)

Top comments (0)