LadderThis is a guided exercise: in a small suite of steps, a program is getting coded in Haskell, by you the reader. This guide will indicate what is to be done at each step, with examples of use of the functions you will create. The how is entirely yours to find out. Import System.Environmentmain = getArgs = checkArgs = readWords = printLadderwhere checkArgs:: String - IO StringcheckArgs args length args 3 = return argscheckArgs args otherwise = error 'usage: ladder ' readWords:: String - IO ( String, String, String)readWords f,s,t = docs length w length s). Words) $ readFile freturn (cs, s, t)printLadder:: ( String, String, String) - IO printLadder (ws,s,t) = putStrLn $ unwords $ ladder ws s tThe definition of the function. Neighbor 'cat ' 'dog ' ⏎ Falseneighbor 'cat ' 'bat ' ⏎ Trueneighbor 'cat ' 'cot ' ⏎ Trueneighbor 'cat ' 'cob ' ⏎ Falseneighbor 'cat ' 'cab ' ⏎ Trueneighbor 'dog ' 'do ' ⏎ Falseneighbor 'at ' 'cat ' ⏎ FalseIf your definition is using recursion: Can you think of an alternative definition that would use a high order function?If your definition is using high order functions: Can you think of an alternative definition that would use recursion?
Off Delay Timer (TOF) Pulse Timer (TP) Timer Accuracy. Depending on which platform you are using you will find quite a lot of different types of timer functions. Many of them are platform specific which means that you can only use them on a certain platform. Siemens for example has many of their own PLC timer functions.
Connecting neighbors 2.1 Neighbors of a wordLet's use our function to retrieve all the words that are neighbor a given word in a word list. This word list will serve as dictionary, so we might as well define a type alias for it. Let ws = words 'bag bat bog cat cog dog fog ' ⏎ let (vs,t) = explore ws 'fog ' $ initialTree 'fog ' ⏎vs ⏎ 'bog ', 'cog ', 'dog 'lookupEdge 'fog ' t ⏎ Just ' 'lookupEdge 'bog ' t ⏎ Just 'fog 'lookupEdge 'cog ' t ⏎ Just 'fog 'lookupEdge 'dog ' t ⏎ Just 'fog ' let (vt,u) = explore ws 'bag ' t ⏎vt ⏎ 'bat 'lookupEdge 'bat ' u ⏎ Just 'bag ' fst $ explore ws 'fog ' u ⏎ 6. Breadth SearchFinding the shortest path between two words involve a breadth first search strategy, meaning that all the neighbors of a word should have been visited before the neighbors of these neighbors are. The search process maintains a queue of words to visit.
Each exploration step extracts the word at the top of the queue, and adds the neighbors of this word at the end of the queue. The process stops either when the list is empty, or when the head of the queue is the word that was sought for.The function. BreadthSearch:: Dictionary - State - Statewill be in charge of this search process. 6.1 Search neighbors, yield new stateLet's start with the first half of the task at hand: implement the breadthSearch function so that when it is given a list of word ws, and a state (v,w.,y,t), it returns a new state (w.y,n,m.,o, u) where:. the words n,m.,o are neighbors of v in ws that are not already present in t. the new tree u has all the neighbor pairs from t plus the neighbor pairs (n,v),(m,v).,(o,v)Here are examples of use of this function. Let ws = words 'dog fog ' ⏎ let search = breadthSearch ws ⏎ fst $ search ( 'fog ',( 'fog ', ' ')) ⏎ 'dog 'fst $ search $ search ( 'fog ',( 'fog ', ' ')) ⏎ fst $ search $ search $ search ( 'fog ',( 'fog ', ' ')) ⏎ Given the word list ws = words 'bag bat cat cot cog dog fog fig' and an initial state st = ('dog',('dog',') how many iterations of search are necessary for the resulting state to contain a path from 'cat' to 'dog'?How many iterations of search are necessary for the resulting state to contain an empty visit queue?
6.3 Stop when a word has been foundSearching a word list ws starting from a word t until there's no more words to visit yields a tree that contains all the possible paths to the word t. If we are only looking for a specific path between a word s and t, this search can represent a lot of overhead.Updtate the function breadthSearch, including its signature. Let ws = words 'dog fog fig ' ⏎ let search = breadthSearch ws 'dog ' ⏎ fst $ search $ initialState 'fog ' ⏎ 'dog ', 'fig 'fst $ search $ search $ initialState 'fog ' ⏎ 'dog ', 'fig 'fst $ search $ search $ search $ initialState 'fog ' ⏎ 'dog ', 'fig ' 6.4 Iterate until the search is doneNow that we have all the different parts of breadthSearch right, the last thing to do is to iterate on the search process. Update the function so that when given a word list ws, a stop word s and state st, it searches ws until its visit queue is empty or the top of the queue is equal to s.Here are new examples of use. Let ws = words 'bog dog fog fig bag bat cat ' ⏎ let (vs,t) = breadthSearch ws 'dog ' $ initialState 'fig ' ⏎ import Data.List ( sort)unwords $ sort $ map fst t ⏎ 'bag bog dog fig fog ' unwords $ path 'dog ' t ⏎ 'dog fog fig ' 7. LadderIn order to determine a minimal ladder between two words from a list, we have to process the word list with our breadthSearch function, starting with an inital state set to (t,(t,')) where t is the last word of the ladder, and then exploit the second half of the resulting state.Write a function. Let ws = words 'bag bat bog cat cog dog fog ' ⏎ unwords $ ladder ws 'bag ' 'fog ' ⏎ 'bag bog fog ' unwords $ ladder ws 'cat ' 'dog ' ⏎ 'cat bat bag bog dog ' unwords $ ladder ws 'foo ' 'dog ' ⏎ ' ' unwords $ ladder ws 'dog ' 'qux ' ⏎ ' 'ws length w 4).
Words) $ readFile 'wordlist.txt ' ⏎ladder ws 'wood ' 'iron ' ⏎ 'wood ', 'good ', 'goad ', 'grad ', 'brad ', 'bran ', 'iran ', 'iron 'Now our program is complete! Here's an example of use: runhaskell Ladder.hs wordlist.txt bread apple ⏎bread tread triad trial trill twill swill still stile stole atole amole ample apple8 Making the program fasterThis program works, but it is disappointingly slow. This is because the neighbors function, which is used at each iteration, filters a large list of words each and every time we have to look for the neighbors of word.Let's profile the execution of the example above ghc -prof -fprof-auto -rtsopts Main.hs ⏎./Main './data/wordlist.txt' apple bread +RTS -p ⏎We can see that the breadthSearch function iterated 1890 times, and neighbor, which is at the heart of the program, was called more than 10 millions times!
COST CENTRE entries%time%alloc%time%allocMAIN 0 0.0 0.0 100.0 100.0main 0 0.0 0.0 99.9 100.0main.checkArgs 1 0.0 0.0 0.0 0.0main.printLadder 1 0.0 0.0 97.6 89.7ladder 1 0.0 0.0 97.6 89.7breadthSearch 1890 0.0 0.0 97.5 89.7breadthSearch.(.) 1889 0.0 0.0 97.2 87.7explore 1889 0.1 0.0 97.2 87.7explore.ns 1889 0.0 0.0 92.7 87.7explore.ns. 7509 0.0 0.0 10.1 0.0lookupEdge 0 10.1 0.0 10.1 0.0neighbors 1889 3.2 0.0 82.6 87.6neighbor 10635065 79.4 87.6 79.4 87.6explore.u 1889 0.0 0.0 4.4 0.0explore.u. 2271 0.0 0.0 4.4 0.0insertEdge 2271 4.4 0.0 4.4 0.0breadthSearch.u 1889 0.0 0.0 0.0 0.0breadthSearch.vs' 1889 0.3 1.9 0.3 1.9breadthSearch.w 1889 0.0 0.0 0.0 0.0breadthSearch.ns 1542 0.0 0.0 0.0 0.0path 15 0.0 0.0 0.1 0.0lookupEdge 0 0.1 0.0 0.1 0.0initialState 1 0.0 0.0 0.0 0.0initialTree 1 0.0 0.0 0.0 0.0main.readWords 1 2.1 9.8 2.3 10.3main.readWords. Import Data.List as L ⏎import Data.Set as S ⏎ws Set String which given a word, yields a set of all the possible variations on that word (hence not including the word itself). For instance, applying this function to the word dog would yield the set of following words:aog bog cog dag dbg dcg ddg deg dfg dgg dhg dig djg dkg dlg dmg dng doa dob doc dod doe dof doh doi doj dok dol dom don doo dop doq dor dos dot dou dov dow dox doy doz dpg dqg drg dsg dtg dug dvg dwg dxg dyg dzg eog fog gog hog iog jog kog log mog nog oog pog qog rog sog tog uog vog wog xog yog zog 9.1 Prefixes and suffixes of a wordCreate a function.