Wednesday, November 07, 2007

Strings in Kawa (nerd post)

Another random nerd post for the three other people on Earth who will want the answer to this question, concerning Kawa, an implementation of Scheme that compiles to Java bytecode, and allows Java and Scheme code to call each other freely, and which I love more and more each day, but whose documentation could use a few more examples for the newcomer. To convert from a Java String (java.lang.String) to a Scheme string, all you need to do is:
(gnu.lists.FString:new some-java-string)
Kawa represents a Scheme string (which is mutable) as a gnu.lists.FString, not as a java.lang.String (which is immutable), which it uses to represent Scheme symbols. So the Scheme primitive string? will return true for an FString, and not for a java.lang.String. Also, Kawa will turn a scheme string literal, like "hello", into a java.lang.String if used where a java.lang.String is expected, for example as a method argument. Took me a while to figure all this out. So:
> (string? (gnu.lists.FString:new (java.lang.String "hello")))
#t

> (string? (gnu.lists.FString:new (java.lang.String:new 'hello)))
#t
For example, to obtain the stack trace of a java exception in Kawa (this whole example isn't necessary, but it also illustrates how to use try-catch in Kawa, which a fellow newbie might find useful),
(define (broken-function-that-returns-stack-trace)
   ;; will throw an exception because 'b is not a list
   (try-catch (display (car 'b))  
              ;; catch java.lang.Throwable, 
              ;; binding it to a variable called exception
              (exception <java.lang.Throwable>
                         ;; and return the exception's stack trace 
                         ;; as a scheme string
                         (java-exception-stack-trace exception))))))

(define (java-exception-stack-trace exception)
  ;; the standard java rigamarole
  ;; needed to obtain an exception's stack trace
  (let* ((string-writer (java.io.StringWriter:new))
         (print-writer (java.io.PrintWriter:new string-writer)))
    (java.lang.Throwable:printStackTrace exception print-writer)
    (java.io.PrintWriter:flush print-writer)
    ;; here's the String-related bit.  convert the java.lang.String
    ;; from the StringWriter into a gnu.lists.FString
    (gnu.lists.FString:new
     (java.lang.Object:toString string-writer))))
So:
> (let ((stack-trace (broken-function-that-returns-stack-trace)))
    (string? stack-trace))
#t
Also, because Kawa represents Scheme symbols as java.lang.Strings (because both are immutable),
> (symbol? (java.lang.String:new "hello"))
#t

> (java.lang.String:toCharArray 'hello)
[h e l l o]
Keep on truckin, my partners in Scheme. Wherever you are.

Thursday, November 01, 2007

When Pismo Won't Start (how to restart it)

How to restart a dead Powerbook G3 Firewire (Pismo).

I've had my Powerbook G3 in the closet for a couple years, and when I pulled it out it simply wouldn't start. I pushed the button, nothing happened. I reset the PMU, nothing happened. I bought a new yo-yo adapter, pushed the power button, nothing. I tried charging it overnight, nothing again. Then I happened upon this blog post, which contained the magic nugget of information that I've distilled here.

1) Unplug the power cord.

2) Open the keyboard, remove the battery and whatever's in the right-hand drive bay (probably a CD/DVD drive).

3) Disconnect the internal battery from the logic board as in the picture above. The round brown thing is the internal battery, the green circuit board the logic board, the red/white/black twisted cable connecting them the thing you want to disconnect. Gently pull the cable out of the white rectangular socket on the logic board as shown in the picture.

4) Plug in the power adapter.

5) Say "BY THE POWER OF GREYSKULL!" and push the power button.

6) Your Mac will start up with the familiar pompous chime.

7) While the computer is still on, plug the internal battery back into the logic board.

8) Leave it on overnight to recharge the internal and removable batteries.

9) Wake up to a new day and start using your Powerbook again (though you will probably have to replace the removable battery like me because it's dead after a couple years without charging). I recommend installing Debian, which works quite well on this older computer. I can't speak for OS X - I'm running Tiger on a Powerbook G4 and it's not quite snappy enough for me; lots of spinning beach balls. I may install Debian on that too.

NOTE: I am not responsible for anything that happens to you or your computer as a result of following these instructions. They worked for me, and seem to have worked for the people on that other blog post.