Re: Suggestion: Syntactic sugar for Maps!

circularfunc:
I suggest Haskell introduce some syntactic sugar for Maps.
Python uses {"this": 2, "is": 1, "a": 1, "Map": 1}
Clojure also use braces: {:k1 1 :k2 3} where whitespace is comma but commas are also allowed.
I find the import Data.Map and then fromList [("hello",1), ("there", 2)] or the other form that I forgot(because it is to long!) to be to long...
So why not {"hello": 1, "there": 2} ?
A comment from the peanut gallery: I took circ's comment to be a suggestion that we adopt an _idiom_. That you can non-idiomatically accomplish the same thing in Haskell by defining some datatypes and functions doesn't seem to address the core suggestion. I'd rewrite circ's suggestion as follows: A bunch of modern and popular languages use the idiom of braces for maps (e.g. { 'a' => 1, 'b' => 2} ). Its simplicity has made maps vital parts of most programs. Much of the world has adopted this idiom and if Haskell adopts this syntactic sugar it will make it easier for others to adopt Haskell. Haskell is really interesting for a number of reasons. Map syntax isn't one of them but map syntax sugar would make adoption of Haskell that much easier. As I think Don S said, it's hard enough to adopt Haskell, so let's make it easier when possible... - Alson

Hi Alson,
So why not {"hello": 1, "there": 2} ?
A comment from the peanut gallery: I took circ's comment to be a suggestion that we adopt an _idiom_. That you can non-idiomatically accomplish the same thing in Haskell by defining some datatypes and functions doesn't seem to address the core suggestion.
I'd rewrite circ's suggestion as follows: A bunch of modern and popular languages use the idiom of braces for maps (e.g. { 'a' => 1, 'b' => 2} ). Its simplicity has made maps vital parts of most programs. Much of the world has adopted this idiom and if Haskell adopts this syntactic sugar it will make it easier for others to adopt Haskell.
I am fairly certain someone could write the necessary magic so: do {'a' ~> 1; 'b' ~> 2} becomes a map without any changes to the language at all. It seems like throwing syntax at a problem should be a last resort. I often do: let (*) = (,) in ['a' * 1, 'b' * 2] I find that quite elegant as an associative list, which you can then convert to a map, a hash table, use as an associative list etc. I also think that those who are looking for Haskell will have their mind so blown by lazy evaluation that keeping their maps similar isn't so necessary :-) Thanks Neil

Am Sonntag, 14. Dezember 2008 15:35 schrieb Neil Mitchell:
I am fairly certain someone could write the necessary magic so:
do {'a' ~> 1; 'b' ~> 2}
becomes a map without any changes to the language at all. It seems like throwing syntax at a problem should be a last resort. I often do:
let (*) = (,) in ['a' * 1, 'b' * 2]
I find that quite elegant as an associative list, which you can then convert to a map, a hash table, use as an associative list etc.
I also think that those who are looking for Haskell will have their mind so blown by lazy evaluation that keeping their maps similar isn't so necessary :-)
Thanks
Neil
+1

FWIW, state monad works fine. And I imagine the QuasiQuote extension
could get get rid of the double quotes on strings (and recover the use
of the colon?).
module Sugar ((~>), build, Builder) where
import Data.Map (Map); import qualified Data.Map as Map
import Control.Monad.State
(~>) :: Ord k => k -> a -> Builder k a
k ~> a = do
m <- get
let m' = Map.insert k a m
put m'
return m'
type Builder k a = State (Map k a) (Map k a)
build :: Builder k a -> Map k a
build x = evalState x Map.empty
m = build $ "zero" ~> 0 >> "one" ~> 1 >> "two" ~> 2
n = build $ do
"zero" ~> 0
"one" ~> 1
"two" ~> 2
o = build $ do { "zero" ~> 0; "one" ~> 1; "two" ~> 2 }
On Sun, Dec 21, 2008 at 7:28 PM, Wolfgang Jeltsch
Am Sonntag, 14. Dezember 2008 15:35 schrieb Neil Mitchell:
I am fairly certain someone could write the necessary magic so:
do {'a' ~> 1; 'b' ~> 2}
becomes a map without any changes to the language at all. It seems like throwing syntax at a problem should be a last resort. I often do:
let (*) = (,) in ['a' * 1, 'b' * 2]
I find that quite elegant as an associative list, which you can then convert to a map, a hash table, use as an associative list etc.
I also think that those who are looking for Haskell will have their mind so blown by lazy evaluation that keeping their maps similar isn't so necessary :-)
Thanks
Neil
+1 _______________________________________________ Haskell-prime mailing list Haskell-prime@haskell.org http://www.haskell.org/mailman/listinfo/haskell-prime
participants (4)
-
Alson Kemp
-
Neil Mitchell
-
Nicolas Frisby
-
Wolfgang Jeltsch