<div dir="ltr"><div>Sticking with the Person example, we might want to use a dynamically-bound function to specify a different greeting message for different values of Person:<br><br>data Person = Person { name :: String , greet :: Person -> String }
<br><br>anne = Person "Anne" (\p -> "Howdy, " ++ name p ++ ".")
<br>bob = Person "Bob" (\p -> "Hi, " ++ name p ++ "! I'm Bob")
<br>carol = Person "Carol" (\_ -> "'Sup?")
<br><br>main = do
<br> putStrLn $ anne `greet` bob<br> putStrLn $ bob `greet` carol<br> putStrLn $ carol `greet` anne
<br><br></div>You could even get really crazy and construct the greeting function at runtime:<br><br>main = do
<br> putStr "Enter a name: "<br> n <- getLine<br> putStrL"Enter a greeting: "<br> greeting <- getLine<br><br> let user = Person n (\p -> concat [greeting, ", ", name p, "!"])<br> bob = Person "Bob" (\p -> concat ["Hello, ", name p, "!"])
<br><br> putStrLn $ bob `greet` user
<br> putStrLn $ user `greet` bob
<br><br>$ runhaskell test.hs
<br>Enter a name:
Morbo
<br>Enter a greeting:
I WILL DESTROY YOU<br>Hello, Morbo!<br>I WILL DESTROY YOU, Bob!<br><br><div class="gmail_quote">On Tue, 5 May 2015 at 08:38 Shishir Srivastava <<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Brandon, <div><br></div><div>Thanks for your response. My example was just a bad turnout which I conjured up using tutorials and playing with it. </div><div><br></div><div>I was going to follow up my question with the possible practical use of why and where someone would use such a construct to wrap a function inside a new data-type. </div><div><br></div><div>For all that matters I could have used 'length' function directly to get the same output. </div><div><br></div><div>I appreciate that you already have given the practical example but anything more basic for beginners to highlight the usage would be helpful.</div><div><br></div><div>Thanks,<div class="gmail_extra"><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif" size="2">Shishir</font></font><br></font><br></div></div></div>
<br><div class="gmail_quote">On Tue, May 5, 2015 at 4:28 PM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks Matthew - that was crisp !<div><br></div><div>Cheers,<br clear="all"><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif" size="2">Shishir</font></font></font><br></div></div></div>
</div></div>
</blockquote></div><br></div></div></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div></div>