slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CS 341 Programming Language Design and Implementation PowerPoint Presentation
Download Presentation
CS 341 Programming Language Design and Implementation

Loading in 2 Seconds...

play fullscreen
1 / 8

CS 341 Programming Language Design and Implementation - PowerPoint PPT Presentation


  • 132 Views
  • Uploaded on

CS 341 Programming Language Design and Implementation. Administrative Quiz #3 friday Topics: programming in F#; event-driven programming Today: Discuss HW6 — image processing in F# Practice for Quiz3. Thinking functionally…. [ a; b; c; d; . . . ]. When to use map ?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS 341 Programming Language Design and Implementation' - sema


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

CS 341

Programming Language Design and Implementation

  • Administrative
    • Quiz #3 friday
      • Topics: programming in F#; event-driven programming
  • Today:
    • Discuss HW6 — image processing in F#
    • Practice for Quiz3

CS 341 -- 05 Mar 2014

thinking functionally
Thinking functionally…

[ a; b; c; d; . . . ]

  • When to use map?
    • when you have to convert a list to a list
  • When to use reduce?
    • when you have to reduce a list to a single value
  • When to use recursion?
    • when the above don't apply

[ s; t; u; v; . . . ]

[ a; b; c; d; . . . ]

r

[ a; b; c; d; . . . ]

.Tail

.Head

CS 341 -- 05 Mar 2014

exercise
Exercise
  • write a function satisfies that takes a function F and a list L, and counts how many elements eÎ L "satisfy" F — i.e. F(e) returns true

recursive?

let rec satisfies(F,L) =

???

tail-recursive?

higher-order?

let zero(x) =

if x = 0 then true else false

let R = satisfies(zero, [0;1;2;0;-8])

printfn "%A" R // 2

CS 341 -- 05 Mar 2014

solutions
Solutions

let rec satisfies(F,L) =

let rec satisfies(F,L) =

let rec satisfies(F,L) =

hw6 image processing in f
HW6: Image Processing in F#

[

[0; 0; 0; 100; 0; 0; 0; 0; 0; 255; 0; 255; . . . ] ;

[0; 0; 0; 0; 255; 175; 0; 0; 0; 0; 0; 0; . . . ] ;

[0; 0; 0; 0; 0; 0; 0; 15; 175; 0; 0; 0; . . . ] ;

.

.

.

]

CS 341 -- 05 Mar 2014

transformgrayscale
TransformGrayscale:
  • Since list to list, use map
    • but use recursion on row since operating on 3 elements, not 1

let rec private Row2Gray(row) =

match row with

| [ ] -> []

| _ -> let avg = (row.Head + row.Tail.Head + row.Tail.Tail.Head) / 3

avg::avg::avg::Row2Gray(row.Tail.Tail.Tail)

let TransformGrayscale(image:int list list) =

List.map Row2Gray image

let rec TransformGrayscale(image:int list list) =

match image with

| [ ] -> []

| _ -> Row2Gray(image.Head) :: TransformGrayscale(image.Tail)

CS 341 -- 05 Mar 2014

transforminvert
TransformInvert:
  • Use map at both levels
    • also a good example of currying…

let private InvertRow depth row =

List.map (fun x -> depth – x) row

let TransformInvert(depth:int, image:int list list) =

List.map (InvertRow depth) image

CS 341 -- 05 Mar 2014

transformfliphorizontal
TransformFlipHorizontal:

let R(row:int list) = row.Head

let G(row:int list) = row.Tail.Head

let B(row:int list) = row.Tail.Tail.Head

let Next(row:int list) = row.Tail.Tail.Tail

let rec private FlipRowHorz(row) =

match row with

| [ ] -> []

| _ -> let pixel = [R(row); G(row); B(row)]

FlipRowHorz(Next(row)) @ pixel

Slow: 10-15 seconds…

(append builds new list every time)

let TransformFlipHorizontal(image:int list list) =

List.map FlipRowHorz image

Fast! 1 second — prepend does not build new list…

let rec private FlipRowHorz newrow row =

match row with

| [ ] -> newrow

| _ -> FlipRowHorz (R(row)::G(row)::B(row)::newrow) (Next(row))

let TransformFlipHorizontal(image:int list list) =

List.map (FlipRowHorz[]) image