Today we will look at some of the basic logic functions in the Clojure API.
There is a library for logic programming called core.logic, which you can look at here. I think that one gets into some hardcore Prolog-like stuff.
user.core=> (or (string? 1) (string? "1")) true user.core=> (or (string? 1) (string? 1)) false ;; it's like "or" and "||" in other languages, but you can also return values ;; returns true if first two args come out to true user.core=> (or (string? 1) (string? "1") :a :b :c) true user.core=> (or (string? 4) (string? "3") false :b :c) true ;; otherwise, if first two args come out as false, returns first true sort of value user.core=> (or (string? 1) (string? 1) :a :b :c) :a user.core=> (or (string? 4) (string? 3) false :b :c) :b user.core=> (or (int? :a) (int? "h") false :b :c) :b ;; so I guess that could save you an "if" call? user.core=> (or (int? :a) (int? "h") (println "hello") false :b :c) hello :b ;; so it looks like everything gets executed user.core=> (or (int? :a) (int? "h") (println "hello") false :b :c (println "goodbye")) hello :b user.core=> (or (int? :a) (int? "h") false :b (println "hello") :c (println "goodbye")) :b ;; or at least until you get to the thing that returns true ;; if the first two args of "and" return true, then it just returns the last arg user.core=> (and (string? 1) (string? "w") :a :b :c) false user.core=> (and (string? "1") (string? "w") :a :b :c) :c user.core=> (and (string? "1") (string? "w") :a :b :c false) false user.core=> (and (string? 1) (string? "w") true :a :b :c) false user.core=> (and (string? "1") (string? "w") :a :b :c true) true user.core=> (and (string? "1") (string? "w") :a :b :c 2) 2 user.core=> (and (string? "1") (string? "w") :a :b :c) :c ;; you can use it like a regular old "and" from other languages user.core=> (and (string? "1") (string? "w")) true user.core=> (and (string? 1) (string? "w")) false ;; let's try "if" user.core=> (if (string? "1") #_=> (str "This is the true part") #_=> (str "This is the false part")) "This is the true part" user.core=> (if (string? 1) #_=> (str "This is the true part") #_=> (str "This is the false part")) "This is the false part" user.core=> (if (string? 1) #_=> (str "this is the true part") #_=> (str "this is the false part")) "this is the false part" ;; to do more than one thing in a branch, use "do" ;; since each branch only gets one statement/expression user.core=> (if (string? "1") #_=> (do #_=> (str "this is the first true part\n") #_=> (str "this is the second true part")) #_=> (do #_=> (str "this is the first false part\n") #_=> (str "this is the second false part"))) "this is the second true part" ;; it returns the last statement; to actually execute, in this case we need println ;; because println returns nil, and str returns a string user.core=> (if (string? "1") #_=> (do #_=> (println "this is the first true part") #_=> (str "this is the second true part")) #_=> (do #_=> (println "this is the first false part\n") #_=> (str "this is the second false part"))) this is the first true part "this is the second true part" user.core=> (if (string? 1) #_=> (do #_=> (println "this is the first true part") #_=> (str "this is the second true part")) #_=> (do #_=> (println "this is the first false part") #_=> (str "this is the second false part"))) this is the first false part "this is the second false part" ;; "when" is like if without the else user.core=> (when (string? "1") #_=> (str "This is what happens when....you use when")) "This is what happens when....you use when" user.core=> (when (string? 1) #_=> (str "This is what happens when....you use when")) nil user.core=> (when (string? "1") #_=> (println "Do these parens make my program look fat?") #_=> (str "This is what happens when....you use when")) Do these parens make my program look fat? "This is what happens when....you use when" user.core=> (when (string? 1) #_=> (println "Do these parens make my program look fat?") #_=> (str "This is what happens when....you use when")) nil ;; nil? returns true if something is nil user.core=> (nil? 1) false user.core=> (nil? false) false user.core=> (nil? (println "Hello")) Hello true user.core=> (nil? (str "Is nil just a million dollar mistake?")) false user.core=> (nil? nil) true
You’re welcome.