Jeel Shah
Jeel Shah

Reputation: 3334

How can I retrieve the value of a key in a HashMap that is a list in Clojure?

I'm looking to retrieve a value given the key which is a list.

    (defn string-tuple [my-string]
      (partition 2 1 my-string))

    (defn split-and-frequent-tuple [lines]
      (frequencies (string-tuple lines)))

    (defn split-and-frequent [lines]
      (frequencies lines))

(defn string-split [my-string]
  (str/split my-string #" "))


    (def string-pairs (split-and-frequent-tuple (string-split "<s> I am Sam </s> <s> Sam I am </s> <s> I do not like green eggs and ham </s>")))

    ;; Calculate that bigram probability
    (println string-pairs)
    (println (string-pairs '(green eggs)))

Basically, string-pairs counts the frequencies of the number of items something like (green eggs) appears in a text. However, when I try to retrieve the key using something like (string-pairs '(green eggs)) I always get nil back even though the map shows there is a value of 1 for that key.

I was just wondering where I am going wrong, I've tried everything :(

Thanks for your time

Upvotes: 2

Views: 130

Answers (1)

noisesmith
noisesmith

Reputation: 20194

When you use println to display values, strings and symbols look the same, even though they are not equal.

When you are debugging, use prn which unambiguously shows values, or use pr-str to generate the string pr or prn would use, inside your println call.

This should give you the correct result (vectors and lists are equal in Clojure if their contents are equal).

(string-pairs ["green" "eggs"])

Upvotes: 2

Related Questions