Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, I'll <strong>emphasise</strong> getting yourself a good Haskell <a href="http://learnyouahaskell.com/" rel="nofollow">tutorial</a> and working through it. Haskell folks are fairly helpful when it comes to answering questions, provided you show some effort at attempting solutions. I'll let you off this one time. :-)</p> <p>Let's pick this apart one by one: You're writing a chatterbot with a running loop where you'd be posing questions and getting back answers. IOW, your chat bot would go on waiting for questions and spitting out answers at random as soon as it gets one. </p> <pre><code>chatterbot :: String -&gt; [(String, [String])] -&gt; IO () </code></pre> <p>Here's the type definition of your <code>chatterbot</code> function. This says that it accepts a <code>String</code> and a list of pairs each of <code>(String, [String])</code> returning some <code>IO</code> operation - something like <a href="http://www.haskell.org/haskellwiki/Introduction_to_IO" rel="nofollow"><code>void</code></a> in this case. </p> <p>Let's dive into our <code>botloop</code>:</p> <pre><code> botloop = do putStr "\n: " question &lt;- getLine answer &lt;- stateOfMind brain putStrLn (botName ++ ": " ++ (present . answer . prepare) question) </code></pre> <p>As I said above, this is a continuous looping function and it runs as long as the question posed by the chatterbot user does not <em>signal end of dialog</em>. This is important because otherwise your loop does not have a terminating condition and it would just go on forever and ever. It's here:</p> <pre><code> if (not . endOfDialog) question -- If the question does not mean endOfDialog then botloop -- go into the botloop else return () -- else exit this function. </code></pre> <p>Back to <code>botloop</code> - Here you need to ask yourself a few questions:</p> <ul> <li><p>What does <code>stateOfMind</code> mean? Particularly, what's the type of <code>stateOfMind</code>?</p> <pre><code> answer &lt;- stateOfMind brain </code></pre></li> </ul> <p>Here, I get an answer from <code>brain</code> based on it's <code>stateOfMind</code>. Now, what is it? I leave you figure that one out as it's part of your homework. :-)</p> <ul> <li><p>What does <code>brain</code> give me? </p> <pre><code>brain = rulesCompile botRules </code></pre></li> </ul> <p><code>brain</code> is nothing but a compilation of all <code>botRules</code> - It must be specified in your input to <code>chatterbot</code> function. Now, <code>rulesCompile</code> is another function that compiles all of your <code>botRules</code> to yield a <code>BotBrain</code> - this is your ever persistent state machine that holds all of your chatbot's answers/quips/cheekiness. :-)</p> <ul> <li><p>How do you present your chatbot's answer to your user? It's here: </p> <p><code>(present . answer . prepare) question)</code></p></li> </ul> <p>This is a function composition using the composition operator <a href="http://www.haskell.org/haskellwiki/Compose" rel="nofollow"><code>(.)</code></a>. The link explains this better than I can. You have three functions, <code>prepare</code>, <code>answer</code> and <code>present</code>. You get the <code>question</code>, <code>prepare</code> it to be fed into <code>answer</code> and later <code>present</code> it back to your user. <code>prepare</code> deals with some form of printing back results to your end user and hence returns a type wrapped into <code>IO</code>. </p> <p>Now, getting to your questions:</p> <blockquote> <blockquote> <p>Can anyone explain why we have do after = (chatterbot botName botRules = do ...) What is botloop? Is it a label as in C? Whats the functionality of where in this code? Is it possible to write it without where? "botloop = do ..." what does this mean?</p> </blockquote> </blockquote> <p>I explained <code>botloop</code> above. As for the role of the <code>where</code> clause, you can find it in any haskell tutorial. For example, <a href="http://www.haskell.org/haskellwiki/Function_composition" rel="nofollow">HaskellWiki</a> here gives you some information. </p> <blockquote> <blockquote> <p>can anyone explain this condition (not . endOfDialog) question ? Is endOfDialog function that takes question as an argument if so then why is it inside a parenthesis ?</p> </blockquote> </blockquote> <p>It's function composition. Look at the types of <code>not</code> and <code>endOfDialog</code> and piece them together. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload