Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Haskell Online

duplicateElements:: [a]->[a]
duplicateElements []=[]
duplicateElements (x:xs)=x:x:duplicateElements xs
main = print(duplicateElements [1..10])

sommer

:t "a"

gggg

main = putStrLn "hello world"
nod :: Int -> Int -> Int
nod a b | (a<0) = nod (-a) b
        | (b<0) = nod a (-b)
        | (a>b) = nod (a-b) b
        | (a<b) = nod a (b-a)
        | (a==0) = b
        | (b==0) = a
        | (a==b) = a
nok :: Int -> Int -> Int
nok c d | (c==0)&&(d==0) = 0
        | (c>0)&&(d>0) = c*d `div` (nod c d)
        | (c<0)||(d<0) = abs(c*d) `div` (nod c d)

Compile and Execute Haskell Online

import Control.Monad
import Data.Array.Unboxed
import Data.Array.ST
import Data.List

main = print $ result

lim = 1000000

result = sum $
         map multiple $
         takeWhile (\(p,q) -> p <= lim) $
         pairs

multiple :: (Integer,Integer) -> Integer
multiple (p,q) = q * ((p * modpow m q t) `mod` m)
    where e = truncate $ 1 + logBase 10 (fromIntegral p) 
          m = 10^e
          t = ((4*m) `div` 10) - 1

pairs = drop 2 $ zip primes (tail primes) 

-- Power for integers modulo m
modpow :: Integral a => a -> a -> a -> a
modpow m a b = foldl' step 1 (binary b)
    where step acc x = if x==0 then acc^2 `mod` m
                               else (a * acc^2) `mod` m

-- Binary representation
binary :: Integral a => a -> [a]
binary = reverse . binary'
    where binary' n | n<0 = error "binary: negative number"
                    | n <= 1 = [n]  
                    | otherwise = if n `rem` 2 == 0
                                    then 0 : binary' (n `div` 2)
                                    else 1 : binary' (n `div` 2)

primes = [p | (p,True) <- assocs $ primeWheelSieve 1100000]

primeWheelSieve :: Integer -> UArray Integer Bool
primeWheelSieve limit = runSTUArray $ do
  arr <- newArray (1, limit) False
  writeArray arr 2 True
  writeArray arr 3 True
  writeArray arr 5 True
  forM_ [6 .. limit] (\n -> when (n `rem` 6 == 1 || n `rem` 6 == 5) $
                                writeArray arr n True)
  forM_ [5 .. isqrt limit] $ \p -> do 
      isPrime <- readArray arr p
      when isPrime $ do
          let pp = (p+1) - (p+1) `rem` 6
          (forM_ [p*(pp-1), p*(pp+5) .. limit]
                 (\n -> writeArray arr n False))
          (forM_ [p*(pp+1), p*(pp+7) .. limit]
                 (\n -> writeArray arr n False))
  return arr

isqrt :: Integral a => a -> a
isqrt n = truncate . sqrt . fromIntegral $ n

Haskell Zippers

data List a = Empty | Cons a (List a) deriving (Show, Read, Eq, Ord)
type Zipper_List a = ([a],[a])    

go_Forward :: Zipper_List a -> Zipper_List a   
go_Forward (x:xs, bs) = (xs, x:bs)   
   
go_Back :: Zipper_List a -> Zipper_List a   
go_Back (xs, b:bs) = (b:xs, bs)    

main = do 
   let list_Ex = [1,2,3,4] 
   print(go_Forward (list_Ex,[]))       
   print(go_Back([4],[3,2,1])) 

Haskell Monads

main = do
   print([1..10] >>= (\x -> if odd x then [x*2] else []))

Haskell Monoids

multi:: Int->Int 
multi x = x * 1 
add :: Int->Int 
add x = x + 0 

main = do  
   print(multi 9)  
   print (add 7)

Haskell Applicative Functor

import Control.Applicative 

f1:: Int -> Int -> Int 
f1 x y = 2*x+y  
main = do  
   print(show $ f1 <$> (Just 1) <*> (Just 2) ) 

Haskell fmap functor

main = do 
   print (fmap  (+7)(Just 10)) 
   print (fmap  (+7) Nothing)

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.