Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module assignment2
- open System
- let downto1 n = if n <= 0 then [] else List.init n (fun i -> n-i)
- let rec downto11 n = if n <= 0 then [] else n :: downto11 (n-1)
- let downto2 n =
- match n with
- | 0 -> []
- | x when x>0 -> List.init n (fun i -> n-i)
- | _ -> failwith "can't create negative size list"
- let downto22 n =
- let rec downto22 n xs =
- match xs with
- | xs when n > 0 -> downto22 (n-1) [n]@xs
- | _ -> xs
- downto22 n []
- let rec downto222 = function
- | 0 -> []
- | n when n > 0 -> n :: downto222 (n-1)
- | _ -> failwith "can't create negative size list"
- let removeOddIdx xs = List.filter (fun i -> (i%2 > 0)) xs
- let rec combinePair = function
- | [] -> []
- | [x] -> []
- | a::ax::[] -> [(a,ax)]
- | a::ax::axx -> (a,ax) :: (combinePair axx)
- type BCur1 = int*int*int
- let mkCur1 a b c = BCur1(a,b,c)
- let BCurtest = mkCur1 2 2 2
- let BCurtest2 = mkCur1 3 3 3
- let toPence (a,b,c) =
- let shillings = a*20+b
- shillings*12+c
- let fromPence pence =
- if pence <= 0 then (0,0,0) else
- let pounds = pence/(20*12)
- let pence = pence-(pounds*20*12) // Pence = x = fratrukket pounds
- let shillings = pence/12
- let x = pence-(shillings*12)
- (pounds, shillings, x)
- let (.+.) x y =
- match x,y with
- | (a,b,c),(d,e,f) -> fromPence (toPence (a+d, b+e, c+f))
- let (.-.) x y =
- let pence = toPence x - toPence y
- fromPence pence
- let toString1 (x, y, z) = x.ToString() + " pounds, " + y.ToString() + " shillings, and " + z.ToString() + " pence"
- let test = (toString1 ((mkCur1 10 10 10) .+. (mkCur1 3 2 3) .+. (mkCur1 3 2 1)))
- // 16 pounds, 15 shillings, and 2 pence
- type BCur2 = {pounds:int;
- shillings:int
- ;pence:int}
- let toString2 (x:BCur2) = x.pounds.ToString() + " pounds, " + x.shillings.ToString() + " shillings, and " + x.pence.ToString() + " pence"
- let mkCur2 a b c = { pounds = a; shillings = b; pence = c}
- let bcurtest = {pounds = 2; shillings = 2; pence = 2}
- let bcurtest2 = {pounds = 3; shillings = 3 ; pence = 3}
- let BCure2Values x = (x.pounds, x.shillings,x.pence)
- let (..+..) x y =
- match x,y with
- | BCur2(a,b,c),BCur2(e,f,g) -> let s = fromPence (toPence ((a+e),(b+f),(c+g)))
- match s with
- | (a,b,c) -> mkCur2 a b c
- let (..-..) x y =
- let wallet = fromPence((toPence (BCure2Values x)) - toPence (BCure2Values y))
- match wallet with
- | (a,b,c) -> mkCur2 a b c
- type complex = float * float
- let mkComplex x y = complex(x,y)
- let (|+|) x y = mkComplex (fst x + fst y) (snd x + snd y)
- let (|*|) x y = mkComplex ((fst x * fst y)-(snd x * snd y)) ((snd x * fst y)+(fst x * snd y))
- 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))))
- let (|-|) x y = x |+| (mkComplex -(fst y) -(snd y))
- let test1 = (toString2 ((mkCur2 10 10 10) ..+.. (mkCur2 3 2 3) ..+.. (mkCur2 3 2 1)))
- // x/y = x*(1/y)
- (*
- let rec altsum = function
- | [] -> 0
- | [x] -> x
- | x0::x1::xs -> x0 - x1 + altsum xs
- *)
- let rec explode1 (s:string) = Array.toList (s.ToCharArray())
- let rec explode2 (s:string) =
- let l = s.Length
- if l <= 1 then [s.[0]] else
- explode2 (s.Substring(0, (l-1))) @ [s.[l-1]]
- let implodeTest = ['h';'e';'j']
- let rec implode list = List.foldBack (fun c s -> (c.ToString()) + s ) list ""
- let rec implodeRev list = List.fold (fun s c -> (c.ToString())+s) "" list
- let toUpper s = implode (List.map Char.ToUpper (explode1 s))
- let toUpper1 = explode1 >> List.map Char.ToUpper >> implode
- let toUpper2 s = explode1 s |> List.map (fun i -> Char.ToUpper(i)) |> implode
- let cTu = Char.ToUpper
- let cIL = Char.IsLetter
- let sub (s:string) (x,y) = s.Substring(x,y)
- let rec palindrome1 (s:string) =
- match s with
- | 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)))
- | x when x.Length > 1 -> if (cTu s.[x.Length-1] = cTu s.[0]) then (palindrome1 (sub s (1,x.Length-2))) else false
- | x when x.Length = 2 -> if cTu x.[0] = cTu x.[1] then true else false
- | _ -> true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement