Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 4.26 KB | None | 0 0
  1. module assignment2
  2.  
  3. open System
  4. let downto1 n = if n <= 0 then [] else List.init n (fun i -> n-i)
  5.  
  6. let rec downto11 n = if n <= 0 then [] else n :: downto11 (n-1)
  7.  
  8. let downto2 n =
  9.     match n with
  10.     | 0 -> []
  11.     | x when x>0 -> List.init n (fun i -> n-i)
  12.     | _ -> failwith "can't create negative size list"
  13.  
  14. let downto22 n =
  15.     let rec downto22 n xs =
  16.         match xs with
  17.         | xs when n > 0 -> downto22 (n-1) [n]@xs
  18.         | _ -> xs
  19.     downto22 n []
  20.  
  21. let rec downto222 = function
  22. | 0 -> []
  23. | n when n > 0 -> n :: downto222 (n-1)
  24. | _ -> failwith "can't create negative size list"
  25.  
  26.  
  27. let removeOddIdx xs = List.filter (fun i -> (i%2 > 0)) xs
  28.  
  29. let rec combinePair = function
  30. | [] -> []
  31. | [x] -> []
  32. | a::ax::[] -> [(a,ax)]
  33. | a::ax::axx -> (a,ax) :: (combinePair axx)
  34.  
  35. type BCur1 = int*int*int
  36. let mkCur1 a b c = BCur1(a,b,c)
  37.  
  38. let BCurtest = mkCur1 2 2 2
  39. let BCurtest2 = mkCur1 3 3 3
  40.  
  41. let toPence (a,b,c) =
  42.     let shillings = a*20+b
  43.     shillings*12+c
  44.  
  45. let fromPence pence =
  46.         if pence <= 0 then (0,0,0) else
  47.         let pounds = pence/(20*12)
  48.         let pence = pence-(pounds*20*12) // Pence = x = fratrukket pounds  
  49.         let shillings = pence/12
  50.         let x = pence-(shillings*12)
  51.         (pounds, shillings, x)
  52. let (.+.) x y =
  53.     match x,y with
  54.     | (a,b,c),(d,e,f) -> fromPence (toPence (a+d, b+e, c+f))
  55. let (.-.) x y =
  56.     let pence = toPence x - toPence y
  57.     fromPence pence
  58.  
  59. let toString1 (x, y, z) =  x.ToString() + " pounds, " + y.ToString() + " shillings, and " + z.ToString() + " pence"
  60.  
  61. let test = (toString1 ((mkCur1 10 10 10) .+. (mkCur1 3 2 3) .+. (mkCur1 3 2 1)))
  62. // 16 pounds, 15 shillings, and 2 pence
  63. type BCur2 = {pounds:int;
  64.               shillings:int
  65.               ;pence:int}
  66. let toString2 (x:BCur2) =  x.pounds.ToString() + " pounds, " + x.shillings.ToString() + " shillings, and " + x.pence.ToString() + " pence"
  67.    
  68. let mkCur2 a b c = { pounds = a; shillings = b; pence = c}
  69. let bcurtest = {pounds = 2; shillings = 2; pence = 2}
  70. let bcurtest2 = {pounds = 3; shillings = 3 ; pence = 3}
  71.  
  72. let BCure2Values x = (x.pounds, x.shillings,x.pence)
  73. let (..+..) x y =
  74.     match x,y with
  75.     | BCur2(a,b,c),BCur2(e,f,g) -> let s = fromPence (toPence ((a+e),(b+f),(c+g)))
  76.                                         match s with
  77.                                         | (a,b,c) -> mkCur2 a b c
  78.  
  79. let (..-..) x y =
  80.         let wallet = fromPence((toPence (BCure2Values x)) - toPence (BCure2Values y))
  81.         match wallet with
  82.         | (a,b,c) -> mkCur2 a b c
  83.    
  84.  
  85. type complex = float * float
  86.  
  87. let mkComplex x y = complex(x,y)
  88.  
  89. let (|+|) x y = mkComplex (fst x + fst y) (snd x + snd y)
  90. let (|*|) x y = mkComplex ((fst x * fst y)-(snd x * snd y)) ((snd x * fst y)+(fst x * snd y))
  91. let (|/|) x y =  x |*| (mkComplex (fst y/(fst y)*(fst y)+(snd y)*(snd y)) (-1.0*(snd y)/((fst y)*(fst y)+(snd y)*(snd y))))
  92.  
  93. let (|-|) x y = x |+| (mkComplex -(fst y) -(snd y))
  94.  
  95. let test1 = (toString2 ((mkCur2 10 10 10) ..+.. (mkCur2 3 2 3) ..+.. (mkCur2 3 2 1)))
  96. // x/y = x*(1/y)
  97.  
  98. (*
  99. let rec altsum = function
  100. | [] -> 0
  101. | [x] -> x
  102. | x0::x1::xs -> x0 - x1 + altsum xs
  103. *)
  104. let rec explode1 (s:string) = Array.toList (s.ToCharArray())
  105.  
  106. let rec explode2 (s:string) =
  107.         let l = s.Length
  108.         if l <= 1 then [s.[0]] else
  109.         explode2 (s.Substring(0, (l-1))) @ [s.[l-1]]
  110. let implodeTest = ['h';'e';'j']
  111. let rec implode list = List.foldBack (fun c s -> (c.ToString()) + s ) list ""
  112.  
  113. let rec implodeRev list = List.fold (fun s c -> (c.ToString())+s) "" list
  114.  
  115. let toUpper s = implode (List.map Char.ToUpper (explode1 s))
  116. let toUpper1 = explode1 >> List.map Char.ToUpper >> implode
  117. let toUpper2 s = explode1 s |> List.map (fun i -> Char.ToUpper(i)) |> implode
  118.  
  119. let cTu = Char.ToUpper
  120. let cIL = Char.IsLetter
  121. let sub (s:string) (x,y) = s.Substring(x,y)
  122. let rec palindrome1 (s:string) =
  123.         match s with
  124.         | x when x.Length > 0 && (not (cIL(x.[x.Length-1]) && cIL(x.[0]))) -> if cIL(x.[0]) then (palindrome1 (sub s (0,x.Length-2))) else (palindrome1 (sub s (1,x.Length-1)))
  125.         | x when x.Length > 1 -> if (cTu s.[x.Length-1] = cTu s.[0]) then (palindrome1 (sub s (1,x.Length-2))) else false
  126.         | x when x.Length = 2 -> if cTu x.[0] = cTu x.[1] then true else false
  127.         | _ -> true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement