### Subversion checkout URL

You can clone with HTTPS or Subversion.

• 3 commits
• 3 files changed
• 1 contributor
Mar 05, 2012
`addes solution to exercise 6.7` `7447c28`
Mar 06, 2012
`added wall climing problem` `dc3c6bb`
`Merge branch 'master' of github.com:ollielo/Haskell` `b11e54f`
1  .gitignore
 ... ... @@ -0,0 +1 @@ 1 +*~
25 DynamicProgramming/maxLongStrategy.hs
 ... ... @@ -0,0 +1,25 @@ 1 +import Data.Array 2 + 3 +-- Exercise 6.7 in Algorithm Design 4 +-- Finding the maximum profit with a long (buy then sell) trade for a given 5 +-- sequence of prices. 6 + 7 +maxLongStrategy ps = table 8 + where 9 + -- bounds for tables 10 + n = length ps 11 + bnds = (0, n-1) 12 + -- turn ps into Array for faster indexing 13 + prices = listArray bnds ps 14 + -- table of maximal profit we can achive at time step i 15 + table = array bnds [(i, profit i) | i <- range bnds] 16 + -- The maximum profit made at time step i is either: 17 + -- 1. if the price is rising, the max profit made at (i-1) plus the 18 + -- increase in price form (i-1) to i. 19 + -- 2. if the price is falling, the max profit made at (i-1). 20 + profit 0 = 0 21 + profit i 22 + | 0 < diff = table!(i-1) + diff 23 + | otherwise = table!(i-1) 24 + where 25 + diff = prices!i - prices!(i-1)
50 DynamicProgramming/wallClimbing.hs
 ... ... @@ -0,0 +1,50 @@ 1 +import Data.Array 2 +import Data.Tuple 3 + 4 +-- Wall climbing example from Modification of CSC 364S Note, Univ of Toronoto, Fall 2003 5 +climbing wall = (fst min_exit, (reverse . solution . snd) min_exit) 6 + where 7 + bnds = bounds wall 8 + left = snd \$ fst bnds -- left edge of the wall 9 + right = snd \$ snd bnds -- right edge of the wall 10 + top = fst \$ snd bnds -- top edge of the wall 11 + -- table for minimum total cost at each brick 12 + table = array bnds [(ij, cost ij) | ij <- range bnds] 13 + -- the cost at the bottom of the wall 14 + cost (1, col) = wall!(1, col) 15 + -- the minimum total cost at brick (i, j) is the minimum of the three lower 16 + -- bricks plus the cost of the current brick. 17 + cost (row, col) 18 + | col == left = brick + min down lower_right 19 + | col == right = brick + min down lower_left 20 + | otherwise = brick + minimum [lower_left, down, lower_right] 21 + where 22 + brick = wall!(row, col) 23 + down = table!(row-1, col) 24 + lower_left = table!(row-1, col-1) 25 + lower_right = table!(row-1, col+1) 26 + -- the total cost of bricks at the top row 27 + top_row = map (table!) (range ((top, left), (top, right))) 28 + -- find the brick with minimum total cost on the top row 29 + min_exit = minimum (zip top_row (range ((top, left), (top, right)))) 30 + -- back tracing to find the path from top to bottom, it has to be reversed 31 + -- before printing. 32 + -- TODO: how can we use unfoldr/iterate/fix to do the recursion? 33 + solution (1, col) = [(1, col)] 34 + solution (row, col) = (row, col) : recur (row, col) 35 + where 36 + recur (row, col) 37 + | diff == down = solution (row-1, col) 38 + | diff == lower_left = solution (row-1, col-1) 39 + | otherwise = solution (row-1, col+1) 40 + where 41 + diff = (table!(row, col) - wall!(row, col)) 42 + down = table!(row-1, col) 43 + lower_left = table!(row-1, col-1) 44 + lower_right = table!(row-1, col+1) 45 + 46 +main :: IO () 47 +main = do 48 + let bricks = [3,2,5,4,8,5,7,5,6,1, 4,4,6,2,3,2,8,9,5,8] 49 + wall = listArray ((1,1), (4,5)) bricks 50 + (putStrLn . show . climbing) wall

### No commit comments for this range

Something went wrong with that request. Please try again.