/
hsFuncs.hsc
47 lines (32 loc) · 1015 Bytes
/
hsFuncs.hsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Recursive where
import Foreign.C.Types -- for foreign calls
-- foreign ctype calls
foreign export ccall hailstone_hs :: CInt -> CInt
foreign export ccall fib_hs :: CInt -> CInt
foreign export ccall fact_hs :: CInt -> CInt
hailstone :: Int -> Int
hailstone n
| n `mod` 2 == 0 = n `div` 2
| otherwise = 3*n + 1
hailstone_hs :: CInt -> CInt
hailstone_hs = fromIntegral . hailstone . fromIntegral
hailSeq :: Int -> [Int]
hailSeq 0 = [0]
hailSeq 1 = [1]
hailSeq n = n:(hailSeq(hailstone n))
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib x = (fib (x-1)) + (fib (x-2))
fib_hs :: CInt -> CInt
fib_hs = fromIntegral . fib .fromIntegral
divisors :: Int -> [Int]
divisors n = [i | i <- [2..(n `div` 2)], n `mod` i == 0]
primes :: Int -> [Int]
primes n = [i | i <- [2..n], divisors i == []]
fact :: Int -> Int
fact n = foldl (*) 1 [1..n]
fact_hs :: CInt -> CInt
fact_hs = fromIntegral . fact . fromIntegral