Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created August 15, 2011 05:16
Show Gist options
  • Select an option

  • Save draftcode/1145745 to your computer and use it in GitHub Desktop.

Select an option

Save draftcode/1145745 to your computer and use it in GitHub Desktop.
replicate :: Int -> a -> [a]
replicate n x = [x | _ <- [1..n]]
pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], x^2 + y^2 == z^2]
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
perfects n = [i | i <- [1..n], sum (factors i) - i == i]
find :: Eq a => a -> [(a,b)] -> [b]
find k t = [v | (k',v) <- t, k == k']
positions :: Eq a => a -> [a] -> [Int]
positions k xs = [i | i <- find k (zip xs [0..n])]
where n = length xs - 1
scalarproduct :: [Int] -> [Int] -> Int
scalarproduct xs ys = sum [x * y | (x, y) <- zip xs ys]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment