Difference between revisions of "FP Solution"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "<syntaxhighlight lang="Haskell"> import Data.Char fact1 :: Int -> Int fact1 0 = 1 fact1 n = n * fact1 (n-1) fact2 :: Int -> Int fact2 n | n==0 = 1 | otherwise = n *...")
 
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
import Data.Char
 
 
 
fact1 :: Int -> Int
 
fact1 :: Int -> Int
 
fact1 0 = 1
 
fact1 0 = 1
 
fact1 n = n * fact1 (n-1)
 
fact1 n = n * fact1 (n-1)
  
fact2 :: Int -> Int
+
</syntaxhighlight>
fact2 n | n==0 = 1
 
        | otherwise = n * fact2 (n-1)
 
  
fact3 :: Int -> Int       
 
fact3 n = if n==0 then 1 else n * fact3 (n-1)
 
  
fib :: Int -> Int
+
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
fib 0 = 0
+
Some text in the div here.
fib 1 = 1
+
<syntaxhighlight lang="Haskell">
fib n = tmp n 0 1 where
+
fact1 :: Int -> Int
  tmp 1 _ b = b
+
fact1 0 = 1
  tmp x a b = tmp (x-1) b (a+b)
+
fact1 n = n * fact1 (n-1)
  
gcd' :: Int -> Int -> Int
+
</syntaxhighlight>
gcd' a b | a > b = gcd' (a-b) b
+
Some more text in the div.
        | a < b = gcd' a (b-a)
+
</div>
        | a==b = a
 
  
gcd2 :: Int -> Int -> Int       
+
<div style="clear:both;"></div>
gcd2 a 0 = a
 
gcd2 a b = gcd2 b (a `mod` b)
 
  
isPrime :: Int -> Bool
+
<div class="toccolours mw-customtoggle-myList">Show solution</div>
isPrime 1 = False
 
isPrime y = isPrimeTest y (ceiling (sqrt (fromIntegral y)::Double))
 
  where
 
  isPrimeTest _ 1 = True
 
  isPrimeTest n x | n `mod` x ==0 = False
 
                  | otherwise = isPrimeTest n (x-1)
 
  
length' :: [a] -> Int
+
<div class=" mw-collapsible mw-collapsed" id="mw-customcollapsible-myList">
length' []  = 0
+
Some text in the div here.
length' (_:xs) = 1 + length' xs
+
<syntaxhighlight lang="Haskell">
 +
fact1 :: Int -> Int
 +
fact1 0 = 1
 +
fact1 n = n * fact1 (n-1)
  
sumIt :: [Int] -> Int
+
</syntaxhighlight>
sumIt []  = 0
+
Some more text in the div.
sumIt (x:xs) = x + sumIt xs
+
</div>
 
 
getHead :: [a] -> a
 
getHead (x:_) = x
 
 
 
getLast :: [a] -> a
 
getLast (x:xs) | length xs == 0 = x
 
              | otherwise = getLast xs
 
 
 
isElement :: Eq a => a -> [a] -> Bool
 
isElement _ [] = False
 
isElement a (x:xs) | a == x = True
 
                  | otherwise = isElement a xs
 
                 
 
getTail :: [a] -> [a]                 
 
getTail (_:xs) = xs
 
 
 
getInit :: [a] -> [a]
 
getInit [_] = []
 
getInit (x:xs) = x : getInit xs
 
 
 
combine :: [a] -> [a] -> [a]
 
combine [] y = y
 
combine (x:xs) y = x : combine xs y
 
 
 
max' :: [Int] -> Int
 
max' [x] = x
 
max' (x:y:z) | x > y = max' (x:z)
 
            | otherwise = max' (y:z)
 
  
max2 :: [Int] -> Int
+
<div style="clear:both"></div>
max2 (y:ys) = tmp y ys where
 
  tmp a [] = a
 
  tmp a (x:xs) | x > a = tmp x xs
 
                |otherwise = tmp a xs
 
 
 
reverse' :: [a] -> [a]           
 
reverse' [] = []
 
reverse' (x:xs) = (reverse' xs) ++ [x]
 
 
 
reverse'' :: [a] -> [a]
 
reverse'' n = tmp n  []
 
  where tmp [] ys = ys
 
        tmp (x:xs) ys = tmp xs (x:ys)
 
 
 
take' :: Int -> [a] -> [a]
 
take' 0 _ = []
 
take' _ [] = []
 
take' n (x:xs) = x: take' (n-1) xs
 
 
 
drop' :: Int -> [a] -> [a]
 
drop' 0 x = x
 
drop' _ [] = []
 
drop' n (_:xs) = drop' (n-1) xs
 
 
 
minimum' :: Ord a => [a] -> a -- Is this right?
 
minimum' [x] = x
 
minimum' (x:y:z) | x < y = minimum' (x:z)
 
                | otherwise = minimum' (y:z)
 
 
 
divisors :: Int -> [Int]
 
divisors n = tmp n where
 
  tmp 0 = []
 
  tmp x | n `mod` x == 0 = x: tmp (x-1)
 
        | otherwise = tmp (x-1)
 
 
 
divisors' :: Int -> [Int]
 
divisors' n =  filter (\x -> n `mod` x == 0) [1..n]
 
 
 
divisors'' :: Int -> [Int]
 
divisors'' n =  [x | x<-[1..n], n `mod` x == 0]
 
 
 
zipThem:: [a] -> [b] -> [(a,b)]
 
zipThem (x:xs) (y:ys) = (x,y) : zipThem xs ys
 
zipThem _ _ = []
 
 
 
dotProduct :: [a] -> [b] -> [(a,b)]
 
dotProduct [] _ = []
 
dotProduct (x:xs) ys = tmp ys ++ dotProduct xs ys where
 
  tmp [] = []
 
  tmp (b:bs) = (x,b) : tmp bs
 
 
 
dotProduct' :: [a] -> [b] -> [(a,b)] 
 
dotProduct' xs ys = [(x,y)|x<-xs, y<-ys]
 
 
 
dotProduct'' :: [a] -> [b] -> [(a,b)]
 
dotProduct'' x y =
 
  zip (concat (map (replicate (length y)) x))
 
                    (concat (replicate (length x) y))
 
 
 
 
 
fibonacci :: Int -> Int
 
fibonacci n = fst (tmp n) where
 
  fibStep (a,b) = (b,a+b)
 
  tmp 0 = (0,1)
 
  tmp x = fibStep (tmp (x-1))
 
 
 
allToUpper :: String -> String
 
allToUpper xs = [toUpper x |x<-xs]                   
 
 
 
allToUpper' :: String -> String
 
allToUpper' xs = map toUpper xs
 
</syntaxhighlight>
 

Latest revision as of 10:45, 23 September 2020

fact1 :: Int -> Int
fact1 0 = 1
fact1 n = n * fact1 (n-1)


Some text in the div here.

fact1 :: Int -> Int
fact1 0 = 1
fact1 n = n * fact1 (n-1)

Some more text in the div.

Show solution

Some text in the div here.

fact1 :: Int -> Int
fact1 0 = 1
fact1 n = n * fact1 (n-1)

Some more text in the div.