What does "Map literal must contain an even number of forms" mean?

995 views
Skip to first unread message

bill nom nom

unread,
Dec 4, 2016, 4:03:04 PM12/4/16
to Clojure
;; This works,
(hash-map :a 1 :b 2 )
;; Here's another way to create a hash map, this won't work because map
;; literal must contain even number of forms
{hash-map :c "Turd" :d "More Turd"} What does "Map literal must contain an even number of forms" mean?

lvh

unread,
Dec 4, 2016, 4:06:31 PM12/4/16
to clo...@googlegroups.com
That’s not how you use a map literal. That’s trying to create a map of hash-map to :c, “Turd” to :d, and “More turd” to…. nothing — which is why it breaks. You need an even number of forms because you need a value associated with every key. I don’t know where you got that comment about another way to create a hash map, but it is incorrect.

lvh



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Nutter

unread,
Dec 4, 2016, 6:29:43 PM12/4/16
to clo...@googlegroups.com
​In other languages, curly braces are used to enclose executable statements, so something like { println("Hello world\n"); }​ makes sense. In Clojure, however, curly braces are used to enclose a list of key/value pairs, aka a map. So when  you say 

{hash-map :c "Turd" :d "More Turd"}

you are creating a data structure with 2 and a half pairs. The first pair uses the function hash-map as the key, and :c as the value; then "Turd" as the next key and :d as the value; then "More Turd" as the last key, and no value, causing the error.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

Karel Miarka

unread,
Dec 5, 2016, 4:55:01 AM12/5/16
to Clojure
(hash-map :a 1 :b 2) is equivalent of {:a 1 :b 2} so this will work: {:c "Turd" :d "More Turd"}

larry google groups

unread,
Dec 5, 2016, 10:41:33 AM12/5/16
to Clojure

This is all you need:

{:c "Turd" :d "More Turd"}


(defn turd [] 
{:c "Turd" :d "More Turd"})

larry google groups

unread,
Dec 5, 2016, 10:53:30 AM12/5/16
to Clojure
This:

(defn turd [] 
{:c "Turd" :d "More Turd"})

Would return this map:

{:c "Turd" :d "More Turd"}


This:

(defn config [username password] 
{:u username :p password})

returns a map based on the function arguments.

bill nom nom

unread,
Dec 13, 2016, 11:07:13 PM12/13/16
to Clojure
Ok, I get it now. Thanks!
Reply all
Reply to author
Forward
0 new messages