Skip to content

Instantly share code, notes, and snippets.

@daemonfire300
Created November 8, 2012 15:55
Show Gist options
  • Save daemonfire300/4039666 to your computer and use it in GitHub Desktop.
Save daemonfire300/4039666 to your computer and use it in GitHub Desktop.
-- Aufgabe 1 a)
rangeProduct :: Int -> Int -> Int
rangeProduct m n
| m == n = m
| m < n = m * rangeProduct (m+1) n
| otherwise = 0
fac :: Int -> Int
fac 0 = 1
fac 1 = 1
fac x = rangeProduct 1 x
fac' :: Int -> Int
fac' 0 = 1
fac' n = n * fac' (n -1)
rangeProduct'::Int->Int->Int
rangeProduct' m n
| m==n = n
| m==0 || n==0 = 0
| m<n = div (fac n) (fac (m-1))
| m>n = div (fac m) (fac (n-1))
countNumberInTuple :: Int -> (Int, Int) -> Int
countNumberInTuple k (a,b)
| a == k && b == k = 2
| a == k || b == k = 1
| otherwise = 0
countNumberInTriple :: Int -> (Int, Int, Int) -> Int
countNumberInTriple k (a,b,c)
| a == k && b == k && c == k = 3
| (a == k && b == k) || (a == k && c == k) || (b == k && b == k) = 2
| a == k || b == k || c == k = 1
| otherwise = 0
maxOccurs :: Int -> Int -> (Int, Int)
maxOccurs a b = (max a b, countNumberInTuple (max a b) (a,b))
{-
maxThreeOccurs :: Int -> Int -> Int -> (Int, Int)
maxThreeOccurs a b c
| countNumberInTriple a (a,b,c) > 0 = (maxN, snd(maxOccurs b c) + 1)
| countNumberInTriple b (a,b,c) > 0 = (maxN, snd(maxOccurs a c) + 1)
| countNumberInTriple c (a,b,c) > 0 = (maxN, snd(maxOccurs b a) + 1)
where maxN = max (fst (maxOccurs a b)) (fst (maxOccurs b c))
-}
-- increases value of tuple x,x at point n
increaseByOne :: Int -> (Int, Int) -> (Int, Int)
increaseByOne n (a,b)
| n > 1 = (a, b+1)
| n == 1 = (a+1, b)
| n < 1 = error "Can not increase tuple at less 1"
| otherwise = error "error while increasing tuple"
maxThreeOccurs :: Int -> Int -> Int -> (Int, Int)
maxThreeOccurs a b c
| fst maxAB == a && a > c = maxAB
| fst maxAB == a && a == c = increaseByOne 2 maxAB
| fst maxAB == b && b > c = maxAB
| fst maxAB == b && b == c = increaseByOne 2 maxAB
| c >= a && c >= b = (c, 1+(countNumberInTuple c (a,b)))
where maxAB = maxOccurs a b
matches :: Int -> [Int] -> [Int]
matches n [] = []
matches n a = [x | x <- a, x == n]
elemt :: Int -> [Int] -> Bool
elemt n a = not (null (matches n a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment