-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHaskell
More file actions
83 lines (74 loc) · 2.52 KB
/
Haskell
File metadata and controls
83 lines (74 loc) · 2.52 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Script started on Fri 09 Dec 2016 09:08:11 PM EST
[4mpi[24m:[1m~[0m> cat thing.hs
--import System.Random
irepeat :: (a -> a) -> a -> Int -> a
irepeat f x i = iterate f x !! i
dub :: String -> Char -> String
dub [] c = []
dub (x:xs) c
| x == c = x:x:(dub xs c)
| otherwise = x:(dub xs c)
replace :: [a] -> (Int, Int) -> [a] -> [a]
replace new (lower, upper) list = x ++ new ++ w
where
(x, y) = splitAt lower list
(z, w) = splitAt ((upper - lower)+ 1) y
--found this improvised algorithm at http://stackoverflow.com/questions/21276844/prime-factors-in-haskell
factor :: Int -> [Int]
factor 1 = []
factor n
| factors == [] = [n]
| otherwise = factors ++ factor (n `div` (head factors))
where factors = take 1 $ filter (\x -> (n `mod` x) == 0) [2 .. n-1]
--functions for use in monte carlo
under :: (Double -> Double) -> (Double, Double) -> Bool
under f (x, y) = y < a
where
a = f(x)
--ran :: (Double, Double) -> (Double, Double) -> Int-> (Double, Double)
--ran (mi, ma) (low, high) = (x, y)
--do
--g <- newStdGen
--h <- newStdGen
--where
--(x, t) = randomR (mi, ma) g
--(y, u) randomR (low, high) h
--rans :: (Double, Double) -> (Double, Double) -> Int -> [(Double, Double)]
--monte :: (Double -> Double) -> (Double, Double) -> Double -> IO Double
--monte = do
--let i = 5 :: Double
--r <- random :: IO Double
--let M = max(f(x) | x ∈ [a,b])[4mpi[24m:[1m~[0m> h[Kghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
[?1h=Prelude> :load thing.hs
[?1l>[1 of 1] Compiling Main ( thing.hs, interpreted )
Ok, modules loaded: Main.
[?1h=*Main> irepeat (+1) 3 0
[?1l>3
[?1h=*Main> irepeat (+1) 3 0[K6
[?1l>9
[?1h=*Main> irepeat (+1) 3 6\[K[K[K[K[K[K[K+"[K"!") "yes" 4
[?1l>"yes!!!!"
[?1h=*Main> dub "hi" 'i'
[?1l>"hii"
[?1h=*Main> dub "hi" 'i'[K[K[K[K[K[Kch[K[K[Kcharactor[K[Ker" 'a'
[?1l>"chaaraacter"
[?1h=*Main> dub "the cat in the hat has a fat head" 'a'
[?1l>"the caat in the haat haas aa faat heaad"
[?1h=*Main> replace [1,2,3] (4,5) [100,200,300,400,500,600,700]
[?1l>[100,200,300,400,1,2,3,700]
[?1h=*Main> replace [1,2,3] (2,5) [1,2,3,4,5,6,7,8]
[?1l>[1,2,1,2,3,7,8]
[?1h=*Main> replace [8] (2,3) [10,20,30,40,50,60]
[?1l>[10,20,8,50,60]
[?1h=*Main> factor 12
[?1l>[2,2,3]
[?1h=*Main> factor 50
[?1l>[2,5,5]
[?1h=*Main> factor 70[K2
[?1l>[2,2,2,3,3]
[?1h=*Main> :quit
[?1l>Leaving GHCi.
[4mpi[24m:[1m~[0m> exit
exit
Script done on Fri 09 Dec 2016 09:20:47 PM EST