Search code examples
clojure

Clojure finding rectangle area


(defn -main []
  (println "\nTo compute the area of a rectangle,")
  (print   " enter its width: ") (flush)
  (let [ Width (read) ]
    (assert (>= Width 0) "-main: Width must be positive"))
  (print " enter its height: ") (flush)
  (let [ Height (read) ]
    (assert (>= Height 0) "-main: Height must be positive")
    (printf "\nThe area is %f %f\n\n" (rectangleArea Width Height  ))

I'm new to Clojure and there is an unable to resolve symbol error whenever I'm trying to compile the printf function


Solution

  • Although you provided an incomplete example, after a few changes it seems that your problem is you're using %f to format an integer (java.lang.Long):

    (defn -main []
      (println "\nTo compute the area of a rectangle,")
      (print   " enter its width: ") (flush)
      (let [ Width (read) ]
        (assert (>= Width 0) "-main: Width must be positive"))
      (print " enter its height: ") (flush)
      (let [ Height (read) ]
        (assert (>= Height 0) "-main: Height must be positive")
        (printf "\nThe area is %f %f\n\n" (+ 10 20  ))))
    
    (-main)
    ;;=> 
    1. Unhandled java.util.IllegalFormatConversionException
       f != java.lang.Long
    

    And of course your let blocks are incorrect, so it should be more like this:

    (defn- read-size [label]
      (print  " enter its " label ":")
      (flush)
      (let [num (read)]
        (assert (>= num 0) (str  label " must be positive"))
        num))
    
    (defn -main []
      (println "\nTo compute the area of a rectangle,")
      (flush)
      (let [width (read-size "width")
            height (read-size "height")]
        (printf "\nThe area is %d \n\n" (* width height))))