Commits

Seth Hobson  committed 3a42713

Added F# assignments

  • Participants
  • Parent commits e44dccc

Comments (0)

Files changed (2)

File F#/Assignment1.fs

+// author: Seth Hobson
+// Code was created using Mono and MonoDevelop on Mac OS X
+module Program
+
+open System
+
+// exercise 4.3, short-circuit evaluation
+let num = 4;;
+let deno = 0;;
+let short = deno <> 0 && num / deno > 2;;
+  
+// exercise 4.4, getAge function
+let getAge (_, _, age) = age;;
+
+// exercise 4.5, cons operator associativity
+// The :: operator is right-associative
+let rightNumTest = 1 :: 2 :: 3 :: [];;
+// the following will not work as it expects all elements after [] to be lists
+// let leftNumTest = [] :: 1 :: 2 :: 3;;
+
+// exercise 4.6, boolean "s" function
+let rec s element list = 
+  if list = [] then false
+  else if (List.head list) = element then true
+  else s element (List.tail list);;
+
+// The following function does the same thing as "s" and is far more concise and readable
+let s2 element list = List.exists (fun elem -> elem = element) list;;
+
+// exercise 4.7, deleteFirst function
+let rec deleteFirst item list = 
+  match list with
+  | [] -> []
+  | hd::tl -> if hd = item then tl
+              else hd::deleteFirst item tl;;
+
+// exercise 4.8, quicksort function
+let rec quicksort list =
+  match list with
+  | [] -> []
+  | hd::tl -> let smaller, larger = List.partition (fun y -> y <= hd) tl
+              quicksort smaller @ [hd] @ quicksort larger;;
+
+// exercise 4.9, zip function
+let rec zip L1 L2 = 
+  match L1 with
+  | [] -> if L2 = []
+          then []
+          else [None]
+  | hd::tl -> if L2 = []
+              then [None]
+              else let tailtups = zip tl (List.tail L2)
+                   Some(hd, (List.head L2)) :: tailtups;;
+                   
+// exercise 4.10, unzip function
+let rec unzip list = 
+  match list with
+  | [] -> ([], [])
+  | hd::tl -> let (hdele1, hdele2) = hd
+              let tup = unzip tl
+              let (tuple1, tuple2) = tup
+              (hdele1::tuple1, hdele2::tuple2);;
+
+// exercise 4.11, partition function
+let partition pred list = 
+  let rec tailrec xs cont =
+    match xs with
+      | [] -> ([], []) |> cont
+      | hd::tl when pred hd -> (fun (yes, no) -> hd::yes, no) >> cont |> tailrec tl
+      | hd::tl -> (fun (yes, no) -> yes, hd::no) >> cont |> tailrec tl
+  tailrec list id;;
+
+// fold helper function for the reduce function
+let rec fold func accum list =
+  match list with
+  | [] -> accum
+  | hd::tl -> let result = func accum hd
+              fold func result tl;;
+
+// exercise 4.12, reduce function
+let reduce func list = 
+  match list with
+  | [] -> 0
+  | hd::tl -> fold func hd tl;;
+
+// doesAnyElementSatisfy function
+let rec doesAnyElementSatisfy func list =
+  match list with
+  | [] -> false
+  | hd::tl -> let elem = doesAnyElementSatisfy func tl
+              if (func hd) then true
+              else elem;;
+
+// selectionSort function
+let rec selectionSort list =
+  match list with
+  | [] -> []
+  | l -> let min = List.min l in
+         let rest = List.filter (fun i -> i <> min) l in
+         let sortedList = selectionSort rest in
+         min :: sortedList;;
+
+[<EntryPoint>]
+let main args = 
+  printfn "Short circuit evaluation: %b\n" short
+  
+  let myStatsTuple = ("Seth", "Hobson", 32)
+  printfn "My age from tuple: %d\n" (getAge myStatsTuple)
+  
+  let numList = [1..10]
+  printfn "The list %A contains 1 as an element is: %b\n" numList (s 1 numList)
+  
+  let charList = ['a'; 'b'; 'c'; 'a'; 'd'; 'e'; 'a']
+  printfn "The original character list is %A" charList
+  printfn "After calling deleteFirst the list is %A\n" (deleteFirst 'a' charList)
+  
+  let sampleList = [6; 3; 5; 9; 8; 4; 2; 1; 9]
+  printfn "The list %A contains the following even and odd tuples after partitioning: %A\n"
+    sampleList (partition (fun n -> n % 2 = 0) sampleList)
+    
+  printfn "Zip test for %A and %A yields %A\n" [1..3] ['a'..'c'] (zip [1..3] ['a'..'c'])
+  
+  let testTuple = [(1, 'a'); (2, 'b'); (3, 'c')]
+  printfn "Unzip test for %A yields %A\n" testTuple (unzip testTuple)
+  
+  let unsortedList = [98; 5; 32; 15; 225; 1; 22; 42]
+  printfn "Performing quicksort on the list %A - Sorted: %A\n" unsortedList (quicksort unsortedList)
+  
+  printfn "Performing selectionSort on the list %A - Sorted: %A\n" unsortedList (selectionSort unsortedList)
+    
+  printfn "List %A reduces to: %d\n" [2; 3; 1] (reduce (fun x y -> x * y) [2; 3; 1])
+  //Console.ReadLine() // to suspend operation in VS
+  0

File F#/Quilting.fs

+module Program
+
+open System
+open System.Drawing
+
+//SKELETON QUILT PROJECT
+
+type Texture =  | Arcs
+                | Bands;;
+
+type Direction = | NE
+                 | SE
+                 | SW
+                 | NW;;
+                 
+type square = Texture * Direction;;
+                 
+type Square = 
+  val square : Texture * Direction  
+  new (tex,dir) = { square = (tex,dir) };;
+  
+type Row = 
+  val row : List<Square>
+  new (list) = { row = list };;
+  
+type Quilt = 
+  val quilt : List<Row>
+  new (list) = { quilt = list };;
+   
+let clockwise dir = 
+// WRITE THIS FUNCTION;;
+  
+let turnsq square =
+// WRITE THIS FUNCTION;;
+  
+let rec emptyquilt q =
+  match q with
+  | [] -> true
+  | []::tl -> emptyquilt tl
+  | hd::tl -> false;;
+  
+let rec sew quiltOne quiltTwo = 
+// WRITE THIS FUNCTION;;
+    
+let rec turn x = 
+// WRITE THIS FUNCTION;;
+    
+let unturn x = 
+// WRITE THIS FUNCTION;;
+ 
+let pile x y = 
+// WRITE THIS FUNCTION;;
+  
+let a = [[(Arcs,NE)]];;
+let b = [[(Bands,NE)]];;
+
+let slice = 
+  let aa = pile a (turn (turn a))
+  let bb = pile (unturn b) (turn b)
+  let p = sew bb aa
+  let q = sew aa bb
+  pile p q;;
+    
+let encodeImage square =
+  match square with
+  | Arcs,NE ->  new Bitmap(@"carpetNE.JPG")
+  | Arcs,SE ->  new Bitmap(@"carpetSE.JPG")
+  | Arcs,SW ->  new Bitmap(@"carpetSW.JPG")
+  | Arcs,NW ->  new Bitmap(@"carpetNW.JPG")
+  | Bands,NE -> new Bitmap(@"carpet2NE.JPG")
+  | Bands,SE -> new Bitmap(@"carpet2SE.JPG")
+  | Bands,SW -> new Bitmap(@"carpet2SW.JPG")
+  | Bands,NW -> new Bitmap(@"carpet2NW.JPG");;
+  
+let stitchRowImage r = List.map encodeImage r;;  
+
+let rec calcWidth (r : List<Bitmap>) width = 
+  match r with
+  | [] -> width
+  | hd::td -> let r = calcWidth td width
+              hd.Width + r;;
+              
+let rec calcHeight x = 
+  match x with 
+  | [] -> 0
+  | hd::td -> 80 + calcHeight td;;
+                        
+let calcRowWidth r = calcWidth r 0 ;;
+
+let saveFinalImage (bitmaps : List<List<Bitmap>>) (final : Bitmap) = 
+  let g = Graphics.FromImage(final)
+  let mutable widthOffset = 0
+  let mutable heightOffset = 0
+  for row in bitmaps do
+    widthOffset <- 0
+    for b in row do
+      g.DrawImage(b, new Rectangle(widthOffset, heightOffset, 80, 80))
+      widthOffset <- widthOffset + 80;
+    heightOffset <- heightOffset + 80  
+  final.Save(@"stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);;
+      
+  
+let createQuiltImage x = let bitmaps = List.map stitchRowImage x
+                         let width = calcRowWidth (List.head bitmaps)
+                         let height = calcHeight bitmaps
+                         let final = new Bitmap(width, height)
+                         saveFinalImage bitmaps final;;
+                         
+let quilt1 =
+  let q = sew slice slice
+  sew q slice;;
+  
+let quiltImage = createQuiltImage quilt1;;
+
+[<EntryPoint>]
+let main args = 
+    Console.WriteLine("Hello world!")
+    0
+