문제

I'm having some trouble understanding something in scheme functionality, look at the following code:

(let
  ((a (list (< 10 30) (display "This message should never be printed"))))
  (not (car a)))

2 question rise up, A, why is it when I define a in this manner is the display function running even though I just want it to be an item in the list and not "trigger" it. B, how do I prevent it from running?

도움이 되었습니까?

해결책

To prevent evaluation you need to quote:

(let ((a '((< 10 30) (display "This message should never be printed"))))
  (car a))

(let ((a '((< 10 30) (display "This message should never be printed"))))
  (cdr a))

yields

'(< 10 30)
'((display "This message should never be printed"))

If however you want to evaluate those expressions later, the easiest way is to wrap them into a lambda expression:

(define a 
  (list 
   (lambda () (< 10 30))
   (lambda () (displayln "This message should never be printed"))))

(first a)
=> #<procedure>

((first a))
=> #t

(second a)
=> #<procedure>

((second a))
=> This message should never be printed
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top