
Hi Brandon, Thanks for your response. My example was just a bad turnout which I conjured up using tutorials and playing with it. 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. For all that matters I could have used 'length' function directly to get the same output. I appreciate that you already have given the practical example but anything more basic for beginners to highlight the usage would be helpful. Thanks, Shishir On Tue, May 5, 2015 at 4:28 PM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Thanks Matthew - that was crisp !
Cheers, Shishir

On Tue, May 5, 2015 at 11:38 AM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
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.
For all that matters I could have used 'length' function directly to get the same output.
Because you want a different function for each value, typically. Continuing with the DynamicLog example, I have two different uses of DynamicLog in my xmonad config; one feeds an EWMH panel from the logHook, so it sets defaultPP { {- ... -}, ppOutput = dbusOutput ch } where ch was previously associated with a dbus endpoint. This is executed whenever the focused window changes or the current workspace changes. The second is in a keybinding I use for debugging and outputs to the session log (~/.xsession-errors): defaultPP { {- ... -}, ppOutput = hPutStrLn stderr } (The ellipsis comments customize the value in other ways not important here.) You might think of this use of record syntax as being the Haskell version of "named parameters" (as distinct from positional parameters) present in some other languages. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

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:
data Person = Person { name :: String , greet :: Person -> String }
anne = Person "Anne" (\p -> "Howdy, " ++ name p ++ ".")
bob = Person "Bob" (\p -> "Hi, " ++ name p ++ "! I'm Bob")
carol = Person "Carol" (\_ -> "'Sup?")
main = do
putStrLn $ anne `greet` bob
putStrLn $ bob `greet` carol
putStrLn $ carol `greet` anne
You could even get really crazy and construct the greeting function at
runtime:
main = do
putStr "Enter a name: "
n <- getLine
putStrL"Enter a greeting: "
greeting <- getLine
let user = Person n (\p -> concat [greeting, ", ", name p, "!"])
bob = Person "Bob" (\p -> concat ["Hello, ", name p, "!"])
putStrLn $ bob `greet` user
putStrLn $ user `greet` bob
$ runhaskell test.hs
Enter a name: Morbo
Enter a greeting: I WILL DESTROY YOU
Hello, Morbo!
I WILL DESTROY YOU, Bob!
On Tue, 5 May 2015 at 08:38 Shishir Srivastava
Hi Brandon,
Thanks for your response. My example was just a bad turnout which I conjured up using tutorials and playing with it.
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.
For all that matters I could have used 'length' function directly to get the same output.
I appreciate that you already have given the practical example but anything more basic for beginners to highlight the usage would be helpful.
Thanks, Shishir
On Tue, May 5, 2015 at 4:28 PM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Thanks Matthew - that was crisp !
Cheers, Shishir
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
Alex Hammel
-
Brandon Allbery
-
Shishir Srivastava