Skip to content

Instantly share code, notes, and snippets.

@Show-vars
Last active October 1, 2015 12:35
Show Gist options
  • Save Show-vars/50784cdd6396cbbebe5b to your computer and use it in GitHub Desktop.
Save Show-vars/50784cdd6396cbbebe5b to your computer and use it in GitHub Desktop.
homework.hs
-- factorial number
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- factorial each element
factorialEach [] = []
factorialEach (y:yx) = factorial y : factorialEach yx
-- multiply each element by x
productEachByN x [] = []
productEachByN x (y:yx) = (y * x) : productEachByN x yx
-- multiplys all elements
productEach [] = 1
productEach (y:yx) = y * product yx
-- summ all elements
groupSumm [] = 0
groupSumm (y:yx) = y + groupSumm yx
-- maximum element in list
maximumXs x [] = x
maximumXs x (y:yx)
| x < y = maximumXs y yx
| otherwise = maximumXs x yx
maximumEach [] = 0
maximumEach xs = maximumXs 0 xs
main = do
print(productEachByN 2 [1,2,3,4,5,6,7])
print(productEach [1,2,3])
print(groupSumm [1,2,3,4,5])
print(factorialEach [1,2,3,4,5])
print(maximumEach [1,2,6,4,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment