Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Array/kadane.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- kadane.hs
-- Kadane's algorithm in Haskell

module Main where

-- returns the maximum sum of the substring
kadane :: [Int] -> Int
kadane = snd . foldl step (0, minBound)
where
step (currentMax, globalMax) x =
let newCurrent = max x (currentMax + x)
newGlobal = max globalMax newCurrent
in (newCurrent, newGlobal)

-- Example usage
main :: IO ()
main = do
let arr = [−2,1,−3,4,−1,2,1,−5,4]
putStrLn "Array:"
print arr

let maxSum = kadane arr
putStrLn "\nMaximum subarray sum:"
print maxSum
31 changes: 31 additions & 0 deletions Bits Manipulation/bitReverse.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- bitReverseDemo.hs
-- Demonstracja bitReverse64 w Haskellu

module Main where

import Data.Bits
import Data.Word
import Numeric (showHex)

-- | Bit-reverse for 64-bit words.
bitReverse64 :: Word64 -> Word64
bitReverse64 x0 =
let x1 = ((x0 `shiftR` 1) .&. 0x5555555555555555) .|. ((x0 .&. 0x5555555555555555) `shiftL` 1)
x2 = ((x1 `shiftR` 2) .&. 0x3333333333333333) .|. ((x1 .&. 0x3333333333333333) `shiftL` 2)
x3 = ((x2 `shiftR` 4) .&. 0x0F0F0F0F0F0F0F0F) .|. ((x2 .&. 0x0F0F0F0F0F0F0F0F) `shiftL` 4)
x4 = ((x3 `shiftR` 8) .&. 0x00FF00FF00FF00FF) .|. ((x3 .&. 0x00FF00FF00FF00FF) `shiftL` 8)
x5 = ((x4 `shiftR` 16) .&. 0x0000FFFF0000FFFF) .|. ((x4 .&. 0x0000FFFF0000FFFF) `shiftL` 16)
x6 = (x5 `shiftR` 32) .|. (x5 `shiftL` 32)
in x6

-- Helper to display Word64 in hex
showW64Hex :: Word64 -> String
showW64Hex w = "0x" ++ showHex w ""

-- Example usage
main :: IO ()
main = do
let x = 0x0123456789ABCDEF
putStrLn $ "Original: " ++ showW64Hex x
let rev = bitReverse64 x
putStrLn $ "Bit-reversed: " ++ showW64Hex rev