Did I misunderstand the `cond` syntax

Can someone explain to me what’s wrong with the following code? The compiler
claims: Unable to resolve symbol: union in this context. Have I musunderstood the cond syntax?

I’ve removed most of the real code and replaced with stubs 1, 2, 3, 4, leaving just enough to show the confusing error.

(defn first-types
  ([expr]
   1)

  ([[token & args]]
   (if (nil? args)
     2
     (case token
       :cat (cond
              (nullable (first args))
              (union #{1} #{2})
              
              :else
              3)

       4))))

Screenshot 2020-03-31 at 15.03.05

I figured it out. I need

(require '[clojure.set :refer [union]])

or presumably I can use the following

(ns clojure-rte.core
  (:require [clojure.set :refer [union]])
  (:gen-class))

You are welcome to use ClojureVerse.org for questions such as this, of course. I wanted to point out the existence of the Clojurians Slack at https://clojurians.slack.com in case you were unaware of it. There is a #beginners channel there that is perfect for beginner-type questions and answers.

https://ask.clojure.org/ is also a good place for asking about docs and code.

This is what you’d do in the REPL, correct.

Correct, this is how you would do it in a namespace.

I think I would recommend against refer in this case, and prefer (ns ... (:require [clojure.set :as set]) and then use set/union. In general the norm is to minimize refer for clarity’s sake.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.