Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
evgenii-malov / btree_eq.hs
Last active January 3, 2022 22:39
Check if two trees are equal in haskell
-- watch video https://www.youtube.com/watch?v=YyT1vLinnFk&t=353s
import PrettyT
import Data.List
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
equal :: Eq a => Btree a -> Btree a -> Bool
equal Empty Empty = True
equal Empty (Node _ _ _) = False
equal (Node _ _ _) Empty = False
@evgenii-malov
evgenii-malov / reb.hs
Last active December 18, 2021 07:33
Rebuild a binary tree from inorder and preorder with Haskell
-- subscribe my channel https://www.youtube.com/channel/UCBGYk5s1Fnk0PpqSwtH1M9w
-- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
import PrettyT
import Data.List
-- IMPORTANT NOTE: nodes must be uniq!
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
ino Empty = []
ino (Node a l r) = (ino l) ++ [a] ++ (ino r)
@evgenii-malov
evgenii-malov / left_right_view_bt.hs
Created December 10, 2021 14:34
Left and right view of binary tree
-- subscribe my channel https://www.youtube.com/channel/UCBGYk5s1Fnk0PpqSwtH1M9w
-- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
import PrettyT
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
paths :: Btree a -> [[a]]
paths Empty = []
paths (Node a Empty Empty) = [[a]]
@evgenii-malov
evgenii-malov / bst.hs
Last active February 7, 2022 09:30
Binary search tree in haskell
-- GHCi, version 8.8.4
-- author evgenii malov
-- binary search tree -
-- for every node the all left subtree nodes < and all right subtree node >=
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
import Data.List
import PrettyT
-- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
-- insert
-- https://www.youtube.com/watch?v=TC9YPLyTTDo&t=0s
@evgenii-malov
evgenii-malov / eachable.hs
Last active December 10, 2021 14:43
eachable typeclass for haskell
-- see https://www.youtube.com/watch?v=6OD_lNIPp7w
-- and https://stackoverflow.com/questions/70002842/expand-list-of-lists-by-adding-element-once-to-every-list/70221844#70221844
main = undefined
class Eachable e where
each :: (a -> a) -> e a -> [e a]
instance Eachable [] where
each _ [] = []
each g (x:xs) = ((g x) : xs) : map (x:) (each g xs)
# Concurrency python vs haskell, why haskell do it better?
# see video - https://www.youtube.com/watch?v=5RBxI1fm6wA
#python 3.10.0
#server
import asyncio
async def factorial(n):
fact = 1
i = 0
-- Concurrency python vs haskell, why haskell do it better?
-- see video - https://www.youtube.com/watch?v=5RBxI1fm6wA
-- stack --resolver nightly-2021-11-27 new simpleserver
-- dependencies:
-- - base >= 4.7 && < 5
-- - network
-- - bytestring
-- - binary
-- - monad-loops
@evgenii-malov
evgenii-malov / set_partition.hs
Created November 26, 2021 08:46
set partition in haskell
import Control.Monad
-- [1,2]
-- [ [[1],[2]],[[1,2]] ]
-- [1,2,3]
-- 3 [ [[3],[1],[2]],[[3,1],[2]],[[1],[3,2]], [[3],[1,2]],[[3,1,2]] ]
-- [1,2]
-- [ [[1],[2]],[[1,2]] ]
--3 [[1],[2]] --> [ [[3,1],[2]], [[1],[3,2]] ]
@evgenii-malov
evgenii-malov / cartesian.hs
Created November 19, 2021 17:28
cartesian product in haskell
import Control.Applicative
crt xs = do
x <- xs
y <- xs
return [x,y]
--cr xs = (,) <$> xs <*> xs
-- cr xs = liftA2 (,) xs xs
--cr xs = liftA2 (\x y -> [x,y]) xs xs
@evgenii-malov
evgenii-malov / permutations.hs
Created November 17, 2021 19:02
permutations in haskell
import Data.List
p :: Eq a => [a] -> [[a]]
p [] = [[]]
p xs = xs >>= (\x -> (x:) <$> p (delete x xs) )