This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
revdigits x = go x 0 | |
where go k r | |
| k == 0 = r | |
| otherwise = go d (10*r+m) | |
where (d, m) = k `quotRem` 10 | |
isPalin x = id x == revdigits x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ListNode* uncons (struct ListNode* xxs, struct ListNode** xs) | |
{ | |
struct ListNode* x = xxs; | |
if (xxs) { | |
*xs = xxs -> next; | |
x -> next = NULL; | |
} | |
return x; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g = foldr (.) id chain | |
where chain = replicate 1000000 succ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static bool cache[1024][1024]; | |
class Solution { | |
public: | |
std::string longestPalindrome(std::string s) { | |
int sz = (int)s.size(); | |
if (sz == 0) return s; | |
int start = 0, len = 1; | |
memset(cache, 0, sizeof(cache)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE DataKinds #-} | |
data Nat = Z | Succ Nat | |
deriving Show | |
data Vec :: (* -> Nat -> *) where | |
Nil :: Vec a Z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.ByteString.Char8 as C | |
import Data.Maybe | |
import Text.Printf | |
readPrice = fromJust . readPrice_ | |
where readPrice_ s = C.readInt s >>= \(x, s1) -> | |
C.uncons s1 >>= \(c, s2) -> | |
case c of | |
'.' -> C.readInt s2 >>= \(y, s3) -> return (x*100+y) | |
_ -> Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ViewPatterns #-} | |
import qualified Data.Sequence as Seq | |
import Data.Sequence(Seq) | |
lastr :: Seq a -> Maybe a | |
lastr (Seq.viewr -> Seq.EmptyR) = Nothing | |
lastr (Seq.viewr -> s Seq.:> x) = Just x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE PatternSynonyms #-} | |
import Foreign.C | |
newtype Mode = MkMode {unMode :: CInt } | |
pattern Mode0 = MkMode 0 | |
pattern Mode1 = MkMode 1 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
showIntBase2 = reverse . concatMap show . un | |
where un = unfoldr go | |
go = uncurry (*>) . (&&&) (guard . (>0)) (Just . swap . (`quotRem` 2)) | |
-- > showIntBase2 12 | |
-- "1100" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Tree a = Leaf | Branch a !(Tree a) !(Tree a) | |
inOrder g Leaf = mempty | |
inOrder g (Branch a l r) = g a <> inOrder g l <> inOrder g r | |
instance Foldable Tree where | |
foldMap g = inOrder g | |
treeSum :: Tree Integer -> Integer | |
treeSum = getSum . foldMap Sum |
OlderNewer