diff --git a/futhark/readme.md b/futhark/readme.md new file mode 100644 index 0000000..f734188 --- /dev/null +++ b/futhark/readme.md @@ -0,0 +1 @@ +[Futhark](https://futhark-lang.org/) \ No newline at end of file diff --git a/haskell/scratch/Monadic b/haskell/scratch/Monadic new file mode 100755 index 0000000..5fcdeb5 Binary files /dev/null and b/haskell/scratch/Monadic differ diff --git a/haskell/scratch/Monadic.hi b/haskell/scratch/Monadic.hi new file mode 100644 index 0000000..638e12b Binary files /dev/null and b/haskell/scratch/Monadic.hi differ diff --git a/haskell/scratch/Monadic.hs b/haskell/scratch/Monadic.hs new file mode 100644 index 0000000..92cc3f0 --- /dev/null +++ b/haskell/scratch/Monadic.hs @@ -0,0 +1,77 @@ +-- We want to take a list of divisors and a number +-- We traverse the list of divisors and check if the number is divisible by the divisor +-- If the number is divisible by the return the number divided by the divisor +-- If the number is not divisible repeatedly increment the number by 1 until it is divisible by the divisor +-- If the divisor is 0, throw an error +data MyMonad a = DivByZeroError String | NotDivisible Int | Divisible a deriving (Show) + +-- This is where we define fmap (i.e. map but for monads) +instance Functor MyMonad where + fmap f (DivByZeroError s) = DivByZeroError s + fmap f (Divisible a) = Divisible (f a) + fmap f (NotDivisible a) = NotDivisible a + +-- This is where we define <*> (i.e. apply but for monads) essentially applying the +-- "wrapped" function to the "wrapped" value +instance Applicative MyMonad where + (DivByZeroError s) <*> _ = DivByZeroError s + (Divisible a) <*> b = fmap a b + (NotDivisible a) <*> b = NotDivisible a + pure = Divisible + +-- This is where we define >>=, the bind operator, which chains monadic operations +-- i.e. in this case, we chain the operations of dividing and checking if the number is divisible +instance Monad MyMonad where + DivByZeroError msg >>= _ = DivByZeroError msg -- if we encounter a DivByZeroError, we propagate it forward + NotDivisible n >>= _ = NotDivisible n -- if we encounter a NotDivisible, we propagate it forward + Divisible x >>= f = f x -- if we encounter a Divisible, we apply the function f to the value x + + +-- Try to divide the number n by d +tryDivide :: Int -> Int -> MyMonad Int +tryDivide 0 _ = DivByZeroError "Division by zero" +tryDivide d n + | n `mod` d == 0 = Divisible (n `div` d) + | otherwise = NotDivisible n + +-- Process the divisors (map out the results of tryDivide) +sequentialDividing :: [Int] -> Int -> MyMonad Int +sequentialDividing [] n = return n +sequentialDividing (d:ds) n = do + newN <- tryDivide d n + sequentialDividing ds newN + +main :: IO () +main = do + let divisors = [2, 3, 5, 9, 0] + let number = 420 + -- 420/2 = 210 + -- 210/3 = 70 + -- 70/5 = 14 + -- 14/9 not good => 14 is returned + print $ sequentialDividing divisors number + + let divisors2 = [2, 3, 5, 7, 0] + let number2 = 420 + -- 420/2 = 210 + -- 210/3 = 70 + -- 70/5 = 14 + -- 14/7 = 2 + -- 2/0 not good => error + + print $ sequentialDividing divisors2 number2 + + + let divisors3 = [2, 3, 5, 7, 2] + let number3 = 420 + -- 420/2 = 210 + -- 210/3 = 70 + -- 70/5 = 14 + -- 14/7 = 2 + -- 2/2 = 1, all good => 1 is returned + print $ sequentialDividing divisors3 number3 + +-- [Divisible 210,Divisible 140,Divisible 84,NotDivisible 420,DivByZeroError "Division by zero"] +-- 420 is divisible by: [2,3,5] + +-- Wtf is the point of a monad here? diff --git a/haskell/scratch/Monadic.o b/haskell/scratch/Monadic.o new file mode 100644 index 0000000..b4dec6a Binary files /dev/null and b/haskell/scratch/Monadic.o differ diff --git a/haskell/scratch/monadDumb.hs b/haskell/scratch/monadDumb.hs new file mode 100644 index 0000000..2350bd6 --- /dev/null +++ b/haskell/scratch/monadDumb.hs @@ -0,0 +1,66 @@ +-- error handling with a custom monad +-- say we want to divide a list of numbers by a divisor +-- if the divisor is even, we throw an error, then apply ^2 to the rest of the numbers +-- if the divisor is odd, we return the result +-- if the divisor is 0, we throw an error + +-- custom monad +data MyMonad a = DivByZeroError String | DivByEven a | DivByOdd a deriving (Show) + +-- instance of Functor +instance Functor MyMonad where -- Functor is where we define fmap (i.e. map) + -- customMap, where we have 2 functions, one for even and one for odd + fmap f (DivByZeroError s) = DivByZeroError s + fmap f (DivByEven a) = DivByEven (f a) + fmap f (DivByOdd a) = DivByOdd (f a) + +-- instance of Applicative +instance Applicative MyMonad where + (DivByZeroError s) <*> _ = DivByZeroError s + -- DivByEvenError -> ^2 + (DivByEven a) <*> b = fmap a b -- + (DivByOdd a) <*> b = fmap a b + pure = DivByOdd + +-- instance of Monad +instance Monad MyMonad where + return = pure + (DivByZeroError s) >>= _ = DivByZeroError s + (DivByEven a) >>= f = + case f a of + DivByOdd b -> DivByEven b -- propagate the "evenness context" forward + DivByEven b -> DivByEven b -- keep even state if it continues + DivByZeroError s -> DivByZeroError s + (DivByOdd a) >>= f = f a + +safeDivide :: Int -> [Int] -> MyMonad [Int] +safeDivide 0 _ = DivByZeroError "Division by zero" +safeDivide d xs + | even d = DivByEven (map (^2) xs) + | otherwise = DivByOdd (map (`div` d) xs) + +main :: IO () +main = do + let nums = [69, 420, 666, 1337] + let divisor = 2 + let result = safeDivide divisor nums + + let nums2 = [1,2,3,4,5] + let divisor2 = 0 + let result2 = safeDivide divisor2 nums2 + + let nums3 = [69, 420, 666, 1337] + let divisor3 = 3 + let result3 = safeDivide divisor3 nums3 + + print result + print result2 + print result3 + + + + + + + + diff --git a/haskell/sessions/16_03/scratchpad/Adhoc.java b/haskell/sessions/16_03/scratchpad/Adhoc.java new file mode 100644 index 0000000..01c81fa --- /dev/null +++ b/haskell/sessions/16_03/scratchpad/Adhoc.java @@ -0,0 +1,72 @@ +// Java program to show working of user defined +// Generic classes + +// We use < > to specify Parameter type + +class Cat { + String breed; + String name; + boolean castrated; + + Cat(String breed, String name, boolean castrated) { + this.breed = breed; + this.name = name; + this.castrated = castrated; + } + + public String makeNoise() { + return "Meow"; + } + +} + +class Test { + // An object of type T is declared + T obj; + + Test(T obj) { + this.obj = obj; + } + + public T getObject() { + return this.obj; + } +} + +class AdHocPolymorphic { + public String add(int x, int y) { + return "Sum: " + (x + y); + } + + public String add(String name) { + return "Added " + name; + } +} + +public class Adhoc { + public static void main(String[] args) { + AdHocPolymorphic poly = new AdHocPolymorphic(); + + System.out.println(poly.add(1,2)); // prints "Sum: 3" + System.out.println(poly.add("Jay")); // prints "Added Jay" + + // error the fuck out + } +} + + +// Driver class to test above +class Main { + public static void main(String[] args) { + // instance of Integer type + Test iObj = new Test(15); + System.out.println(iObj.getObject()); + + // instance of String type + Test sObj = new Test("GeeksForGeeks"); + System.out.println(sObj.getObject()); + + Test cObj = new Test(new Cat("Siamese", "Fluffy", true)); + System.out.println(cObj.getObject().makeNoise()); + } +} diff --git a/haskell/sessions/16_03/scratchpad/a b/haskell/sessions/16_03/scratchpad/a new file mode 100755 index 0000000..ef8f89b Binary files /dev/null and b/haskell/sessions/16_03/scratchpad/a differ diff --git a/haskell/sessions/16_03/scratchpad/a.hi b/haskell/sessions/16_03/scratchpad/a.hi new file mode 100644 index 0000000..e319a44 Binary files /dev/null and b/haskell/sessions/16_03/scratchpad/a.hi differ diff --git a/haskell/sessions/16_03/scratchpad/a.hs b/haskell/sessions/16_03/scratchpad/a.hs new file mode 100644 index 0000000..d38ecf2 --- /dev/null +++ b/haskell/sessions/16_03/scratchpad/a.hs @@ -0,0 +1,62 @@ +z :: Int -> Int +z a = 3 * a -- 3x3 = 9 => 3*3 + +y :: (Int -> Int) -> [Int] -> [Int] +-- y(\x -> 3*x) [1,2,3] -> [3,6,9] +y z lst = map z lst -- f(x) = 3x => f x = 3x + +isEven :: Int -> Bool +isEven x = if x `mod` 2 == 0 then True else False + +-- ~~mod(x, 2)~~ => x mod 2 +-- isEven x = x `mod` 2 == 0 + +isAmongus :: String -> String +-- traverse each character and check if "amongus" is in the string, return the rest of the string +isAmongus [] = [] -- base case if the string is empty (or we reached the end of the string) +isAmongus (x : xs) -- recurse on the rest of the string + | (take 7 (x : xs) == "amongus") = drop 7 (x : xs) + | otherwise = isAmongus xs + +b :: [Int] -> Int +b lst = sum (y z lst) -- = 18 + +allTogether :: [Int] -> Int +allTogether list = sum (map (3 *) list) + +foo :: Integer -> Integer +foo 0 = 16 +foo 1 + | "Haskell" > "C++" = 3 + | otherwise = 4 +foo n + | n < 0 = 0 + | n `mod` 17 == 2 = -43 + | otherwise = n + 3 + +bogus :: [a] -> Bool +bogus (x:_) = True +bogus _ = False + + +main :: IO () +main = do + print $ b [1, 2, 3] + putStrLn "AAAAAAAAA" + print $ allTogether [1, 2, 3] + + putStrLn "Finding amongus" + print $ isAmongus "amongus" + print $ isAmongus "aaaaaaaaaaaaaa" + print $ isAmongus "askldhjewkjrhwekjrhwekjrhekjlfhbjerkshtjkernamogusejqwklwqhekqjwehqwjkamongusasdasdsad" + +-- foos + print $ foo 0 + + print $ bogus [1,10..] + + -- compose 2 functions with a dot + print $ (y z . y z) [1, 2, 3] + print $ (y z . y z . y z) [1, 2, 3] + print $ (.) (y z) (y z) [1, 2, 3] + print $ y z $ y z [1, 2, 3] diff --git a/haskell/sessions/16_03/scratchpad/a.o b/haskell/sessions/16_03/scratchpad/a.o new file mode 100644 index 0000000..6db1708 Binary files /dev/null and b/haskell/sessions/16_03/scratchpad/a.o differ