2

I'm learning Clojure using "Clojure From the Ground Up" by Kyle Kingsbury.

The VERY FIRST exercise is to write a Clojure routine to test if a given string is a palindrome.

I wrote something recursive:

enter image description here

==================================================

I put in the various println statements to debug it.

Then I discovered Cursive has a debugger! I tried using it. I can't get the debugger to stop at a breakpoint.

I pull down Run --> Debug... and get the following console output:

==================================================

<>

Connected to the target VM, address: '127.0.0.1:5994', transport: 'socket'
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended


    Top  1234564321
    Nil?  1234564321
    Equal  1234564321

    Top  (2 3 4 5 6 4 3 2)
    Nil?  (2 3 4 5 6 4 3 2)
    Equal  (2 3 4 5 6 4 3 2)

    Top  (3 4 5 6 4 3)
    Nil?  (3 4 5 6 4 3)
    Equal  (3 4 5 6 4 3)

    Top  (4 5 6 4)
    Nil?  (4 5 6 4)
    Equal  (4 5 6 4)

    Top  (5 6)
    Nil?  (5 6)


Exception in thread "main" Syntax error compiling at (C:\Users\Joe Us-er\OneDrive\Chit\CChit\palin\src\palin\palin.clj:24:1).

<>

Disconnected from the target VM, address: '127.0.0.1:5994', transport: 'socket'

Process finished with exit code 1

==================================================

I don't know which is information and which is just data. For now, WHY didn't my program stop at any breakpoints? Thank you.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1/1/2020 OK. I took your advice and wrote something simpler. And STILL no breakpoints. Here's a screen shot:

2-line function with lots of breakpoints that don't get hit.

Note that "Top" and "Bottom" both get println'd, but no breakpoints get hit.

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

1/2/2020 Here's 'stupid' again, with no syntax errors, and still not hitting any breakpoints.

enter image description here

Is there some sort of special namespace I should be referencing for debugging? I seem to remember reading something about that that I didn't understand.

Or is that "64-bit server VM warning" message significant?

LionelGoulet
  • 557
  • 2
  • 13
  • Thank you for this tip, I believe all breakpoints are Enabled. – LionelGoulet Dec 31 '19 at 12:46
  • 1
    Ya, I just booted IntelliJ and noticed that disabled breakpoints use a hollow symbol. I'm not sure what the error is, but the code is quite broken. You can't use `()` for arbitrary grouping like you are with `((println "Nil") (flush) true))`. That will attempt to call the return of `println` (nil) as a function. The first symbol/object in a `()` is a function to be called. – Carcigenicate Dec 31 '19 at 12:49
  • Try running the debugger on simpler code first, like a function with just some calls to `println` as a sanity test. Because of the issue I mentioned above, I'm actually surprised the code is even gives the output that you've posted here. – Carcigenicate Dec 31 '19 at 12:56
  • 2
    Your program is just wrong. It is trying to call the result of println as a function, in the if branches. I guess it just doesn't reach the line with breakpoint. To group side effecting lines in if branches use `do` – leetwinski Dec 31 '19 at 13:36
  • A comment like "Your program is just wrong" is at best not helpful. As I said at the very top of this, I am learning Clojure. I KNOW it's wrong. Kindly tell me how to make it right. In detail if you can. Thank you. – LionelGoulet Jan 01 '20 at 14:10
  • 1
    @LionelGoulet , sorry, i was in the new year rush, while commenting that. Your screenshot shows the following for the true branch of `if` (let me format it in a more conventional way): `((println "Nil") (flush) true))`. As brackets in lisp are not the syntax to group the statements (unlike `{ }` in java/c++/js...) but rather the syntax to call the function being the head of this list with the args, that are the tail. If the forms are also the function calls, call them first, left to right. – leetwinski Jan 01 '20 at 16:09
  • 1
    Having said that, we can read that line as: "call println with argument "Nil", then call flush with no args, and then call the result of first (println call, returns nil) as a function with arg1 == result of flush call (returns nil), and arg2 == true". So it leads us to this: `(nil nil true)` => Exception, The same is valid for the falsey if branch, as well as for the internal if. So what you need to do, to is to use the grouping operator to execute statements line by line, namely `do`: `(do (println "Nil") (flush) true)`, it executes code line by line, returning the result of last statement – leetwinski Jan 01 '20 at 16:19
  • 1
    so `(do ...)` if more or less like `{ }` in other languages, with the difference that it also returns a value (some languages, like rust for example, also do it for `{}` blocks). – leetwinski Jan 01 '20 at 16:22
  • 1
    In both of your outputs shown you have "syntax error compiling ..." so the code is not being compiled fully and therefore will not get run fully and therefore the breakpoints will not be available. – Sean Corfield Jan 01 '20 at 21:25
  • Thank you for the comment about syntax errors. I fixed that by removing an extra parenthesis. And I think I understand about "(nil nil true)" So now I have a "perfect" simple program that STILL doesn't hit the breakpoints. What am I missing? – LionelGoulet Jan 02 '20 at 14:17
  • 1
    While not immediately helpful for you, I do find that the debugger is less useful in Clojure where most functions written are pure and can be tested and tweaked in the REPL. – Kamuela Franco Jan 05 '20 at 11:49

1 Answers1

0

Found it! What's missing from my code is an initial namespace declaration. Once I put this line in:

(ns stupid)

and hit the debug button, the 'stupid' program started up and stopped at the first breakpoint! Yay!

Kudos to Daniel O King for his throwaway comment "Every Clojure file starts with a call to the ns operator," at Medium.com https://medium.com/@daniel.oliver.king

I have seen this documented nowhere else.

LionelGoulet
  • 557
  • 2
  • 13