clojureclojure-contrib

Clojure normalize function runtime error


This code that I wrote gives me the error:

java.lang.Long cannot be cast to clojure.lang.IFn

which means that I am using a number where a function is expected.

I think it has to do with the expt functions from clojure.math.numeric-tower but I am not sure. cryptic error messages FTL.

(ns point-normalize.core
  (:require [clojure.math.numeric-tower :as math :only (sqrt expt)]))

(defn normalize [x y]
  (let [x1 (/ x (math/sqrt ((math/expt x 2)+ (math/expt y 2))))
        y1 (/ y (math/sqrt ((math/expt x 2)+ (math/expt y 2))))]
    (x1 y1)))

Any hints would be appreciated. Thank You.


Solution

  • the + is in the wrong place in:

    ((math/expt x 2)+ (math/expt y 2)))
    

    should be:

    (+ (math/expt x 2) (math/expt y 2)))
    

    and the same for y1 as well. Since you have this correct elsewhere it looks like a simple typo.

    While it's very normal to see ))))))) in clojure code, occurrences of (( warrent a second glance.