What is the error in the following racket code? - racket

;creating traffic signal image
(require 2htdp/image)
(define (bulb c)
(circle 10 "solid" c))
(above (bulb "red")
(bulb "yellow")
(bulb "green"))
The Error Displayed is:. Module Language: there can only be one expression in the definitions window in: (define (bulb c) (circle 10 "solid" c))
Interactions disabled.
///I can't figure out the error

DrRacket is failing to recognize the language being used in your code. You need to choose a language manually. It looks like you are following the first chapters of the HtDP textbook, so I recommend going for the Beginning Student Language. You can do so by:
adding #lang htdp/bsl at the beginning of your file
using the picker at the bottom of the DrRacket window:

Related

Why is racket passing #<void> to my function instead of the state?

I'm learning Racket, and using the book Realm Of Racket. I reached the point where they show you how to animate an image moving down the window, and then suggest you try modifying it to react to a left or right-arrow key press to move the image sideways.
Seemed simple enough...
#lang racket
(require 2htdp/universe 2htdp/image)
(define WIDTH 500)
(define DX 0)
(define HEIGHT 500)
(define IMAGE .) ; The image doesn't show up here
(define (add3-to-state current-state)
(+ current-state 3))
(define (draw-image current-state)
(place-image IMAGE (+ DX (/ WIDTH 2)) current-state (empty-scene WIDTH HEIGHT)))
(define (state-is-500 current-state)
(>= current-state 459))
(define (set-delta current-state key)
(set! DX (+ DX 10)))
(big-bang 0
(on-tick add3-to-state)
(on-key set-delta)
(to-draw draw-image)
(stop-when state-is-500)
)
When I run this, the image moves downwards as before, but as soon as I hit a key, I get an error...
>=: contract violation
expected: real?
given: #<void>
...in the state-is-500 function.
Anyone able to explain why this is happening? As far as I can see, my function for on-key has the same form as the ones they show in the book.
I tried modifying the state-is-500 function to look like this...
(define (state-is-500 current-state)
(printf "~a\n" current-state)
(>= current-state 459))
...and the output I got was...
102
105
#<void>
...before the error happened.
So it looks like #<void> is being passed to the function, but why?
Thanks
The issue is that big-bang expects the function call on-key to return the new state of the game. You are not retuning anything thus the frameworks reports <void>
Thus if the state does not change the you should just return the input state e.g.
(define (set-delta current-state key)
current-state)
Also this framework and Racket itself makes you want to not mutate variables, that is you don't assign to them, or at least you assign to them only once. Thus set! is not something you should use often.
In this case the state of the app that the book provides is just the vertical distance so is just a single number as shown in add3-to-state. The original state is passed in as the first parameter to big-bang.
The change you have to make is that the current state is not just the up-down movement but also the side to side. This movement shows up as the second parameter to place-image. So you have to have a state that can provide the second and third parameters to that.

"Package GLUT does not exist", even though cl-opengl installed in Arch Linux

I have emacs configured with SLIME for developing in Common Lisp (sbcl) on Arch Linux. The thing is, I now want to start working with OpenGL as well, so I've installed cl-opengl to provide the necessary bindings. I have also set up a symlink on .local/share/common-lisp to /usr/share/common-lisp (I should be able to load all systems using ASDF that way).
However, when I try to compile the following code in SLIME (using C-c C-k)
(require :asdf) ; need ASDF to load other things
(asdf:load-system :cl-opengl) ; load OpenGL bindings
(asdf:load-system :cl-glu) ; load GLU bindings
(asdf:load-system :cl-glut) ; load GLUT bindings
(defclass my-window (glut:window)
()
(:default-initargs :width 400 :height 300
:title "My Window Title"
:x 100 :y 100
:mode '(:double :rgb :depth)))
(defmethod glut:display-window :before ((win my-window))
(gl:shade-model :smooth) ; enables smooth shading
(gl:clear-color 0 0 0 0) ; background will be black
(gl:clear-depth 1) ; clear buffer to maximum depth
(gl:enable :depth-test) ; enable depth testing
(gl:depth-func :lequal) ; okay to write pixel if its depth
; is less-than-or-equal to the
; depth currently written
; really nice perspective correction
(gl:hint :perspective-correction-hint :nicest)
)
(defmethod glut:display ((win my-window))
(gl:clear :color-buffer-bit :depth-buffer-bit)
(gl:load-identity))
(defmethod glut:reshape ((win my-window) width height)
(gl:viewport 0 0 width height) ; reset the current viewport
(gl:matrix-mode :projection) ; select the projection matrix
(gl:load-identity) ; reset the matrix
;; set perspective based on window aspect ratio
(glu:perspective 45 (/ width (max height 1)) 1/10 100)
(gl:matrix-mode :modelview) ; select the modelview matrix
(gl:load-identity) ; reset the matrix
)
(glut:display-window (make-instance 'my-window))
I get the following error:
READ error during COMPILE-FILE:
Package GLUT does not exist.
even though cl-glut.asd exists in /usr/share/common-lisp/systems.
What am I doing wrong?
ASDF:LOAD-SYSTEM doesn't take effect until load time, since it's a plain function. If you want the effect to happen at compile time, you have to wrap it in an eval-when form. But it's better to write a system definition that :depends-on those other systems.

Dr Racket get pixmap width/height

I'm working on a project for university and was wondering if it is possible to get the height and width of a pixmap in some way. I know it is possible for rectangles and circles and stuff...
I'm using this to draw the pixmap, file is a jpg:
(define (draw-image! x y file)
(let ((posn (my-make-posn x y)))
((draw-pixmap MainWindow) file posn)))
Perhaps you already know this, but the library you're using is deprecated: in the manual, you'll see that it's part of the "Legacy Library."
Here's a simple program that loads a picture from a jpg, displays it, and prints its dimensions, using the 2htdp/image library:
#lang racket
(require 2htdp/image)
(define pic
(bitmap "/Users/clements/Pictures/Photo Booth Library/Pictures/Photo on 2012-02-12 at 10.53.jpg"))
pic
(image-height pic)
(image-width pic)
You'll probably have to update the path....

How to interactively read in two inputs and use them in a function call

I am currently taking a class to learn elisp so I have no experience with this language. I am trying to interactively read in two inputs (the width and length of a rectangle) and then use them to call a function to compute the area of the rectangle. The code I have is as follows:
(defun rectangle_Area(w l)
"Compute the area of a rectangle, given its width and length interactively."
(interactive "nWidth: ")
(interactive "nLength: ")
(setq area (rectangleArea w l))
(message "The rectangle's area is %f." area))
Currently I get a wrong number of arguments error.
Like I said, I have no previous experience... all I really need to know is how to store/read in two separate values using interactive.
Thank you for any help
C-hf interactive RET:
To get several arguments, concatenate the individual strings,
separating them by newline characters.
So we have:
(defun rectangle_Area(w l)
"Compute the area of a rectangle, given its width and length interactively."
(interactive "nWidth: \nnLength: ")
(setq area (rectangleArea w l))
(message "The rectangle's area is %f." area))

How do I set the size of Emacs' window?

I'm trying to detect the size of the screen I'm starting emacs on, and adjust the size and position the window it is starting in (I guess that's the frame in emacs-speak) accordingly. I'm trying to set up my .emacs so that I always get a "reasonably-big" window with it's top-left corner near the top-left of my screen.
I guess this is a big ask for the general case, so to narrow things down a bit I'm most interested in GNU Emacs 22 on Windows and (Debian) Linux.
If you want to change the size according to resolution you can do something like this (adjusting the preferred width and resolutions according to your specific needs):
(defun set-frame-size-according-to-resolution ()
(interactive)
(if window-system
(progn
;; use 120 char wide window for largeish displays
;; and smaller 80 column windows for smaller displays
;; pick whatever numbers make sense for you
(if (> (x-display-pixel-width) 1280)
(add-to-list 'default-frame-alist (cons 'width 120))
(add-to-list 'default-frame-alist (cons 'width 80)))
;; for the height, subtract a couple hundred pixels
;; from the screen height (for panels, menubars and
;; whatnot), then divide by the height of a char to
;; get the height we want
(add-to-list 'default-frame-alist
(cons 'height (/ (- (x-display-pixel-height) 200)
(frame-char-height)))))))
(set-frame-size-according-to-resolution)
Note that window-system is deprecated in newer versions of emacs. A suitable replacement is (display-graphic-p). See this answer to the question How to detect that emacs is in terminal-mode? for a little more background.
I've got the following in my .emacs:
(if (window-system)
(set-frame-height (selected-frame) 60))
You might also look at the functions set-frame-size, set-frame-position, and set-frame-width. Use C-h f (aka M-x describe-function) to bring up detailed documentation.
I'm not sure if there's a way to compute the max height/width of a frame in the current windowing environment.
Taken from: http://www.gnu.org/software/emacs/windows/old/faq4.html
(setq default-frame-alist
'((top . 200) (left . 400)
(width . 80) (height . 40)
(cursor-color . "white")
(cursor-type . box)
(foreground-color . "yellow")
(background-color . "black")
(font . "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1")))
(setq initial-frame-alist '((top . 10) (left . 30)))
The first setting applies to all emacs frames including the first one that pops up when you start. The second setting adds additional attributes to the first frame. This is because it is sometimes nice to know the original frame that you start emacs in.
Try adding the following code to .emacs
(add-to-list 'default-frame-alist '(height . 24))
(add-to-list 'default-frame-alist '(width . 80))
The easiest way I've found to do that in an X-Window environment is through X resources. The relevant part of my .Xdefaults looks like this:
Emacs.geometry: 80x70
You should be able to suffix it with +0+0 location coordinates to force it to the upper-left corner of your display. (the reason I don't do it is that I occasionnally spawn new frames, and it makes things confusing if they appear in the exact same location as the previous one)
According to the manual, this technique works on MS Windows too, storing the resources as key/value pairs in the registry. I never tested that. It might be great, it might be much more of an inconvenience compared to simply editing a file.
You can also the -geometry parameter when firing up emacs: emacs -geometry 80x60+20+30 will give you a window 80 characters wide, 60 rows high, with the top left corner 20 pixels to the right and 30 pixels down from the top left corner of the background.
On ubuntu do:
(defun toggle-fullscreen ()
(interactive)
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
)
(toggle-fullscreen)
On windows, you could make emacs frame maximized using this function :
(defun w32-maximize-frame ()
"Maximize the current frame"
(interactive)
(w32-send-sys-command 61488))
(setq initial-frame-alist
(append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
initial-frame-alist))
(setq default-frame-alist
(append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
default-frame-alist))
(defun set-frame-size-according-to-resolution ()
(interactive)
(if window-system
(progn
;; use 120 char wide window for largeish displays
;; and smaller 80 column windows for smaller displays
;; pick whatever numbers make sense for you
(if (> (x-display-pixel-width) 1280)
(add-to-list 'default-frame-alist (cons 'width 120))
(add-to-list 'default-frame-alist (cons 'width 80)))
;; for the height, subtract a couple hundred pixels
;; from the screen height (for panels, menubars and
;; whatnot), then divide by the height of a char to
;; get the height we want
(add-to-list 'default-frame-alist
(cons 'height (/ (- (x-display-pixel-height) 200)
(frame-char-height)))))))
(set-frame-size-according-to-resolution)
I prefer Bryan Oakley's settings. However the 'height not work properly in my GNU Emacs 24.1.1.