Ad

Uses NonEmpty to avoid the empty case.

Code
Diff
  • module FindMaxNumber where
    
    import Data.List.NonEmpty
    
    find_max :: Ord a => NonEmpty a -> a
    find_max (x:|xs) = foldr (\ a b -> if a > b then a else b) x xs
    
    • module FindMaxNumber where
    • find_max :: Ord a => [a] -> a
    • find_max [] = error "find_max: Empty list"
    • find_max (x:xs) = foldr (\ a b -> if a > b then a else b) x xs
    • import Data.List.NonEmpty
    • find_max :: Ord a => NonEmpty a -> a
    • find_max (x:|xs) = foldr (\ a b -> if a > b then a else b) x xs

Find maximum element of a list in Haskell

Code
Diff
  • module FindMaxNumber where
    
    find_max :: Ord a => [a] -> a
    find_max = maximum
    • def find_max(arr):
    • return 0
    • module FindMaxNumber where
    • find_max :: Ord a => [a] -> a
    • find_max = maximum
Code
Diff
  • module Scramble where 
    
    import Data.List (sort)
    
    scramble :: String -> String -> Bool
    scramble a b = sort a == sort b
    • module Scramble where
    • import Data.List (sort)
    • scramble :: String -> String -> Bool
    • scramble a b = "Scarmble this up"
    • scramble a b = sort a == sort b
Code
Diff
  • module HelloHaskell where
    
    main :: IO ()
    main = putStrLn "Hello Haskell!"
    • System.Console.WriteLine("Hello, C#!");
    • module HelloHaskell where
    • main :: IO ()
    • main = putStrLn "Hello Haskell!"

Let's test some big primes.

Code
Diff
  • import random
    
    def prime(x, miller_rabin_rounds=10):
        if x <= 1: return False
        if x == 2: return True
        if x == 3: return True
        if x % 2 == 0: return False
        
        return miller_rabin(x, miller_rabin_rounds)
    
    def factor_out_2(q):
        r = 0
        while q % 2 == 0:
            r += 1
            q //= 2
        return r, q
    
    def square_and_multiply(x, n, m):
        y = 1
        while n > 1:
            if n % 2 == 0:
                x = x**2 % m
                n //= 2
            else:
                y = x*y % m
                x = x**2 % m
                n = (n-1)//2
        return x*y % m
    
    def miller_rabin(n, k):
        # Write n = 2^r * d + 1
        r, d = factor_out_2(n - 1)
        
        for _ in range(k):
            a = random.randint(2, n-1)
            x = square_and_multiply(a, d, n)
            if x == 1 or x == n - 1:
                continue
            
            for _ in range(r-1):
                x = x**2 % n
                if x == n - 1:
                    break
            else:
                return False
        return True 
    • def prime(x):
    • counter = 0
    • for i in range(1,x):
    • if x % i is 0:
    • counter += 1
    • if counter is 0:
    • return True
    • else:
    • return False
    • import random
    • def prime(x, miller_rabin_rounds=10):
    • if x <= 1: return False
    • if x == 2: return True
    • if x == 3: return True
    • if x % 2 == 0: return False
    • return miller_rabin(x, miller_rabin_rounds)
    • def factor_out_2(q):
    • r = 0
    • while q % 2 == 0:
    • r += 1
    • q //= 2
    • return r, q
    • def square_and_multiply(x, n, m):
    • y = 1
    • while n > 1:
    • if n % 2 == 0:
    • x = x**2 % m
    • n //= 2
    • else:
    • y = x*y % m
    • x = x**2 % m
    • n = (n-1)//2
    • return x*y % m
    • def miller_rabin(n, k):
    • # Write n = 2^r * d + 1
    • r, d = factor_out_2(n - 1)
    • for _ in range(k):
    • a = random.randint(2, n-1)
    • x = square_and_multiply(a, d, n)
    • if x == 1 or x == n - 1:
    • continue
    • for _ in range(r-1):
    • x = x**2 % n
    • if x == n - 1:
    • break
    • else:
    • return False
    • return True
Code
Diff
  • larger_than_5 = lambda a: a > 5
    • def larger_than_5(a):
    • return a > 5
    • larger_than_5 = lambda a: a > 5