Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Recursion/dynamic programming optimization (Magnus Therning)
2. Re: How to print the name of a variable (Stephen Tetley)
3. Re: How to print the name of a variable (Ozgur Akgun)
4. Re: How to print the name of a variable (Stephen Tetley)
5. Re: How to print the name of a variable (Ozgur Akgun)
----------------------------------------------------------------------
Message: 1
Date: Sat, 20 Mar 2010 08:26:09 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Recursion/dynamic programming
optimization
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 19/03/10 20:41, ivovnenko wrote:
> Hi.
> I tried to solve one of the google code jam's problems using haskel
> http://code.google.com/codejam/contest/dashboard?c=90101#s=p2:
>
> w :: Eq a => [a] -> [a] -> Int
> w text str = w' (reverse text) (reverse str)
> w' :: Eq a => [a] -> [a] -> Int
> -- Init
> w' _ [] = 0
> w' [] _ = 0
> -- For one symbol string
> w' text [a] =
> if head text == a then
> w' (tail text) [a] +1
> else
> w' (tail text) [a]
> -- General case
> w' text str =
> if head str == head text then
> w' (tail text) (tail str) + w' (tail text) str
> else
> w' (tail text) str
>
> and it worked great on small inputs, not on large:
I suspect that a change of type for w' would improve things a bit. The base
cases:
w' n [] _ = n
w' n _ [] = n
w' n (t:ts) [a] =
if t == a
then w' (succ n) ts [a]
else w' n ts [a]
After reading the problem description though I think that thinking hard about
the problem might reveal that there's a more clever way to solve it instead of
the rather brute-force route you've taken so far.
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org
http://therning.org/magnus identi.ca|twitter: magthe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100320/d980c083/signature-0001.bin
------------------------------
Message: 2
Date: Sat, 20 Mar 2010 10:09:33 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
To: Haskell Beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi John
I can't see there is a way of getting the name of a variable.
A Haskell compiler like GHC is fairly free to do what it likes with
/values/, so a variable doesn't necessarily exist in a tangible form
in a program - i.e. it might get compiled away due to constant
folding[*]:
main = let x = 5 in
do { print (10 * x) }
... would likely become:
main = do { print (10 * 5) } -- by constant folding
... and finally
main = do { print 50 } -- by static expression elimination
--
Values not /variables/ - are more tangible. Provided the type of the
value is an instance of the type class Data, you can get at least the
value's type name and its constructor name.
To get something like named variable you would have to make a data
type with two fields one for a name label and one for the value, e.g.
:
data Named a = Named String a
deriving (Show)
valName :: Named a -> String
valName (Name s _) = s
valValue :: Named a -> a
valValue (Name _ a) = a
When you use a variable you want associated with a name, you will have
to be a bit pedantic about always supplying the same name to construct
it:
e.g.: if you have a constant called scale_factor
scale_factor :: Named Int
scale_factor = Named "scale_factor" 10
[At an advanced level you could probably use Template Haskell to make
this easier]
--
Your program would then be something like :
writeFileFor :: Named a -> IO ()
writeFileFor v =
do
let nameOfV = valName v
outh <- openFile (nameOfV ++ ".txt") WriteMode
let outputstring = calculationsFrom (varValue v)
hPutStr outh outputstring
hClose outh
Though it would more succinct to use pattern matching:
writeFileFor :: Named a -> IO ()
writeFileFor (Named name value) =
do
outh <- openFile (name ++ ".txt") WriteMode
let outputstring = calculationsFrom value
hPutStr outh outputstring
hClose outh
As a side issue - having access to variables rather than values seems
an usually prospect for any programming language. I'd guess that it
would effectively prevent the language being compiled or the language
would would have to limit reflective / introspective access only to
global variables.
Best wishes
Stephen
[*] Actually for important reasons GHC does not do that much constant
folding - but it is possibly the easiest optimization to use as an
illustration here.
------------------------------
Message: 3
Date: Sat, 20 Mar 2010 10:25:46 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
To: Stephen Tetley <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
This is what I have in my library. I use it every now and then, when I need
sth with a name. Of course you still have to implement the name function for
that data type, but I've no better way of doing it.
class Show a => HasName a where
-- | accessor method for name
name :: a -> String
-- | a method the update the stored name
updateName :: String -> a -> a
-- | show functions (for this type-class) are assumed to be shown
*without* the name
-- | if you want to show with the name, you must use this function
instead of show
-- | default implementation should be almost always sufficient.
showWithName :: a -> String
-- | prints instead of just showing.
-- | default implementation should be almost always sufficient.
printWithName :: a -> IO()
-- | if "name" is not implemented, just return a notice
name x = "don't know my name: " ++ show x
-- | if "updateName" is not implemented, there is nothing to do but
giving an error
updateName n x = error ("updateName(" ++ n ++ ") not implemented: " ++
show x)
showWithName p = name p ++ ": " ++ show p
printWithName = putStrLn . showWithName
Hope it helps,
On 20 March 2010 10:09, Stephen Tetley <[email protected]> wrote:
> Hi John
>
> I can't see there is a way of getting the name of a variable.
>
> A Haskell compiler like GHC is fairly free to do what it likes with
> /values/, so a variable doesn't necessarily exist in a tangible form
> in a program - i.e. it might get compiled away due to constant
> folding[*]:
>
> main = let x = 5 in
> do { print (10 * x) }
>
> ... would likely become:
>
> main = do { print (10 * 5) } -- by constant folding
>
> ... and finally
>
> main = do { print 50 } -- by static expression elimination
>
> --
>
> Values not /variables/ - are more tangible. Provided the type of the
> value is an instance of the type class Data, you can get at least the
> value's type name and its constructor name.
>
> To get something like named variable you would have to make a data
> type with two fields one for a name label and one for the value, e.g.
> :
>
> data Named a = Named String a
> deriving (Show)
>
> valName :: Named a -> String
> valName (Name s _) = s
>
> valValue :: Named a -> a
> valValue (Name _ a) = a
>
>
> When you use a variable you want associated with a name, you will have
> to be a bit pedantic about always supplying the same name to construct
> it:
>
> e.g.: if you have a constant called scale_factor
>
> scale_factor :: Named Int
> scale_factor = Named "scale_factor" 10
>
> [At an advanced level you could probably use Template Haskell to make
> this easier]
>
> --
>
> Your program would then be something like :
>
> writeFileFor :: Named a -> IO ()
> writeFileFor v =
> do
> let nameOfV = valName v
> outh <- openFile (nameOfV ++ ".txt") WriteMode
>
> let outputstring = calculationsFrom (varValue v)
>
> hPutStr outh outputstring
>
> hClose outh
>
>
> Though it would more succinct to use pattern matching:
>
>
> writeFileFor :: Named a -> IO ()
> writeFileFor (Named name value) =
> do
> outh <- openFile (name ++ ".txt") WriteMode
>
> let outputstring = calculationsFrom value
>
> hPutStr outh outputstring
>
> hClose outh
>
>
>
> As a side issue - having access to variables rather than values seems
> an usually prospect for any programming language. I'd guess that it
> would effectively prevent the language being compiled or the language
> would would have to limit reflective / introspective access only to
> global variables.
>
> Best wishes
>
> Stephen
>
>
>
> [*] Actually for important reasons GHC does not do that much constant
> folding - but it is possibly the easiest optimization to use as an
> illustration here.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100320/bceebe96/attachment-0001.html
------------------------------
Message: 4
Date: Sat, 20 Mar 2010 11:03:35 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
Cc: Haskell Beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Ozgur
Doesn't that associate names with types rather than names with values though:
instance HasName Int where
name = "Int"
> printWithName (1::Int)
Int: 1
> printWithName (70::Int)
Int: 70
I think the same is achievable with Data.Typeable / Data.Data although
how to do it is somewhat buried...
For associating names and values you'd still need to do it 'by hand'
with something like
data Named a = Named String a
deriving (Show)
or
type Named a = (String,a)
Best wishes
Stephen
------------------------------
Message: 5
Date: Sat, 20 Mar 2010 12:08:46 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
To: Stephen Tetley <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
You're right, if you implement the type class independent of the value
you'll end up with associating names with types.
I use it as an interface to your method actually - sorry I didn't make it
clear enough.
So, something like this:
data NamedInt = NamedInt Int String
instance HasName NamedInt where
name (NamedInt _ n) = n
updateName n (NamedInt v _) = NamedInt v n
Now, if you want, you can have a Num instance for Named Int, redirect
everything to the inner-int, and here you go. You can use NamedInt as a Num
seamlessly.
Cheers,
On 20 March 2010 11:03, Stephen Tetley <[email protected]> wrote:
> Hi Ozgur
>
> Doesn't that associate names with types rather than names with values
> though:
>
> instance HasName Int where
> name = "Int"
>
>
> > printWithName (1::Int)
> Int: 1
>
> > printWithName (70::Int)
> Int: 70
>
> I think the same is achievable with Data.Typeable / Data.Data although
> how to do it is somewhat buried...
>
> For associating names and values you'd still need to do it 'by hand'
> with something like
>
> data Named a = Named String a
> deriving (Show)
>
> or
>
> type Named a = (String,a)
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100320/2d5d9498/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 23
*****************************************