Skip to content

Instantly share code, notes, and snippets.

View viercc's full-sized avatar

Koji Miyazato viercc

View GitHub Profile
@viercc
viercc / q.cpp
Last active November 11, 2019 13:36
tmp
[In Haskell]
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr = ......
(example)
foldr (+) 0 [a,b,c] = a + (b + (c + 0))
[In C++]
@viercc
viercc / Output
Created November 23, 2019 02:45
Trying GHCJS
$ ghcjs -O2 test.hs
[1 of 1] Compiling Main ( test.hs, test.js_o )
Linking test.jsexe (Main)
$ (cd test.jsexe/ && node all.js)
(0,1)
(1,2)
(2,4)
(3,8)
(4,16)
(5,32)
-- Breaking first-class-instances
{-#
LANGUAGE
TemplateHaskell,
RankNTypes,
TypeFamilies,
KindSignatures,
FlexibleInstances,
ConstraintKinds
#-}
@viercc
viercc / eval_by_hkd.hs
Created March 22, 2020 07:55
Overkilling again: "Monthly Hask Anything" question Mar 2020
-- https://www.reddit.com/r/haskell/comments/fbfhum/monthly_hask_anything_march_2020/fl4fgek/
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
import Data.Word
import Data.Functor.Identity
@viercc
viercc / hashtable.hs
Last active March 30, 2020 08:41
On hashtables with "undo-able" insert
-- Just wrote down in the web editor.
-- Not tested at all but I hope you get the idea
import qualified Data.HashTable.ST.Basic as HT
import Data.List.NonEmpty(NonEmpty(..))
import qualified Data.List.NonEmpty as NE
type MyHashTable s k v = HT.HashTable s k (NonEmpty v)
insert :: MyHashTable s k v -> k -> v -> ST s ()
{-
https://www.reddit.com/r/haskell/comments/g6kc33/intersection_of_infinite_lists/
-}
import qualified Data.Set as Set
-------- Original and Step 1 --------
intersection :: (Eq a) => [[a]] -> [[a]] -> Int -> [([a], [a])]
intersection l1 l2 i =
[(x, y) | x <- (take i l1), y <- (take i l2), not (common x y == [])]
@viercc
viercc / Summation.hs
Created May 26, 2020 12:59
For discussion about algorithms to solve a hackerrank excercise
module Summation where
import Data.List (sortBy)
import Data.Ord (comparing)
import qualified Data.Vector.Unboxed as V
type Query = (Int, Int, Int)
test1 :: [Query]
test1 =
@viercc
viercc / Lib.hs
Created March 9, 2021 09:09
Answering effectfully<HYPHEN>ou/haskell<HYPHEN>challenges<SLASH>h4-largest-powers
module Lib
( largestPowersInt ) where
class Eq a => Iterable a where
zer :: a
inc :: a -> a
dec :: a -> a
instance Iterable Int where
zer = 0
$ ghc-9.0.1 -fno-code -ddump-to-file -dsuppress-module-prefixes -dsuppress-type-applications -ddump-ds -ddump-splices Main.hs
[1 of 2] Compiling THPatTest ( THPatTest.hs, /tmp/ghc31722_0/ghc_4.o, /tmp/ghc31722_0/ghc_4.dyn_o )
[2 of 2] Compiling Main ( Main.hs, /tmp/ghc31722_0/ghc_2.o, /tmp/ghc31722_0/ghc_2.dyn_o )
$ runghc Main.hs
(2,2,2)
(1,2,1)
@viercc
viercc / sum-mapm.hs
Created July 13, 2021 04:41
summaion by mapM on State vs. by foldl
sum''''' :: Num a => [a] -> a
sum''''' xs = snd $ foldr h (\s -> ((), s)) xs 0
where
h :: Num a => a -> (a -> b) -> a -> b
h x k = \s -> k (s + x)
{-
snd $ foldr h (\s -> ((), s)) xs 0