-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_1_34.clj
28 lines (23 loc) · 1.06 KB
/
ex_1_34.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns sicp.chapter-1.part-3.ex-1-34)
; Exercise 1.34: Suppose we define the procedure
;
; (define (f g) (g 2))
; Then we have
; (f square) ; 4
; (f (lambda (z) (* z (+ z 1)))) ; 6
; What happens if we (perversely) ask the interpreter to evaluate the combination (f f)?
; Explain.
(defn f
[g]
(g 2))
; (f f)
; Error: java.lang.ClassCastException: class java.lang.Long cannot be cast to class clojure.lang.IFn
; (java.lang.Long is in module java.base of loader 'bootstrap';
; clojure.lang.IFn is in unnamed module of loader 'app')
;
; Explanation
; Calling (f f) will cause an error because the function f expects the argument g is a function that takes one argument.
; In your case, when you call (f f f), f tries to call itself with argument 2 (that is, does (f 2)),
; and then this new instance of f tries again to call the argument (which in this context is also f) with 2,
; and so on. This will result in an infinite recursive call and eventually a call stack overflow
; (StackOverflowError).