1

How would one write the following ocaml expression in clojure:

fun x -> (f (x +. dx) -. f x) /. dx

I simplehearted tried the following expression, but that throws an exception:

(defn derivative [dx f]
  (fn [x] 
    (/ (- f [(+ x dx)] f [x]) dx)))

((derivative 0.01 (fn [x] (* x x))) 1)

java.lang.ClassCastException: ableitung$eval5058$fn__5059 cannot be cast to java.lang.Number Numbers.java:135 clojure.lang.Numbers.minus

1
  • I tried to translate the ocaml-example "The power of functions". And mixed the syntaxes of function-calls. Commented Feb 1, 2016 at 23:31

1 Answer 1

4

You need to apply f which you do with (f x y ..). You probably also shouldn't pass a single vector argument into f, although it's impossible to tell without the definition:

(defn derivative [dx f]
  (fn [x] 
    (/ (- (f (+ x dx)) (f x)) dx)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.