[racket-users] How to check if a value is not false

2017-04-15 Thread Angus
I have a function that can return either false or a node.  I have to check four 
permutations:

both nodes false,
left node false, 
right node valid, left node false
both node valid

I am doing it using cond like this:

(define (lines t)
  (cond [(and (false? (node-l t)) (false? (node-r t))) <>]  ;; if no children
[(and (false? (node-l t)) (not (node-r t)))  <>]  ;; 
if left child only
[(and (node-l t) (false? (node-r t))) <> ]  ;; if 
right child only
[else <> ]))  ;; if both children


the first cond line using and and false? works ok and the last cond line using 
else works ok.  But how do I check for the middle two occurrences?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to apply side effects to all elements in a list

2017-03-22 Thread Angus
I am a bit stuck on rendering images in a list.

Here is my code below.

(require 2htdp/image)

(define-struct bear-state (x y rotation size))

(define MTS (empty-scene WIDTH HEIGHT))

BEAR-IMAGE is just a graphic - of a bear.

(define (render-bear b)
  (place-image (scale (bear-state-size b) (rotate (bear-state-rotation b) 
BEAR-IMAGE))  (bear-state-x b) (bear-state-y b) MTS))



(define (render-bears bears)
  (cond [(empty? bears) (place-image (square 0 "outline" "white") 0 0 MTS)]
[else
  (render-bear (first bears))   ; can render 1 bear
 (render-bears (rest bears))]))


render-bear works but I can't get render-bears to work.  What am I doing wrong? 
Any hints?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Why doesn't this maxval function correctly return the maximum value in the list

2017-02-13 Thread Angus Comber
But it has to have the signature maxval lst - it is a homework problem.

So I thought I would need max in the helper function.  Basically to keep an
internal max saved value.  Is there another way?

On 13 February 2017 at 20:38, Matthias Felleisen 
wrote:

>
> Just lift aux out.
>
>
> > On Feb 13, 2017, at 3:18 PM, Angus Comber  wrote:
> >
> > If I change the code to:
> >
> >  (define (maxval lst)
> >(define (aux lst max)
> >  (cond [(null? lst)  max]
> >[(> (car lst) max) (aux (cdr lst) (car lst))]
> >[#t (aux (cdr lst) max)]))
> >(aux lst 0))
> >
> > and comment out #lang racket at the top of the file and select Beginning
> Student as the language then I get error:
> >
> > define: expected only one expression for the function body, but found 1
> extra part
> >
> > Is this because there is a 2nd define in the function?  Or do I need
> some syntax to indicate to run aux at the end of the definition.
> >
> > On 12 February 2017 at 21:34, Matthias Felleisen 
> wrote:
> >
> > > On Feb 12, 2017, at 3:00 PM, Angus  wrote:
> > >
> > > I am a beginner Racket developer by the way.
> > >
> > > Here is my maxval function which is supposed to take a list and return
> the largest integer in the list.
> > >
> > > (define (maxval lst)
> > >   (define (aux lst max)
> > > (cond [(null? lst)  max]
> > >   [(car lst) > max (aux (cdr lst) (car lst))]
> > >   [#t (aux (cdr lst) max)]))
> > >   (aux lst 0))
> > >
> > >
> > > Using the function always returns the last value in the list
> > >
> > >> (maxval (list 1 2 3 5 6 1))
> > > 1
> > >
> > > The code looks correct to me, but I must be missing something.  What
> have I done wrong?
> >
> >
> > Your code is 100% correct, conceptually. Sadly, it’s suffering from a
> mistake that I saw many many times in the 90s and before. And that’s
> precisely why we get beginners started on Beginning Student Language in
> DrRacket instead of plain Racket. Try it out for your next few exercises or
> even this one, to see whether the error message would have helped.
> >
> > — Matthias
> >
> >
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Why doesn't this maxval function correctly return the maximum value in the list

2017-02-13 Thread Angus Comber
If I change the code to:

 (define (maxval lst)
   (define (aux lst max)
 (cond [(null? lst)  max]
   [(> (car lst) max) (aux (cdr lst) (car lst))]
   [#t (aux (cdr lst) max)]))
   (aux lst 0))

and comment out #lang racket at the top of the file and select Beginning
Student as the language then I get error:

define: expected only one expression for the function body, but found 1
extra part

Is this because there is a 2nd define in the function?  Or do I need some
syntax to indicate to run aux at the end of the definition.

On 12 February 2017 at 21:34, Matthias Felleisen 
wrote:

>
> > On Feb 12, 2017, at 3:00 PM, Angus  wrote:
> >
> > I am a beginner Racket developer by the way.
> >
> > Here is my maxval function which is supposed to take a list and return
> the largest integer in the list.
> >
> > (define (maxval lst)
> >   (define (aux lst max)
> > (cond [(null? lst)  max]
> >   [(car lst) > max (aux (cdr lst) (car lst))]
> >   [#t (aux (cdr lst) max)]))
> >   (aux lst 0))
> >
> >
> > Using the function always returns the last value in the list
> >
> >> (maxval (list 1 2 3 5 6 1))
> > 1
> >
> > The code looks correct to me, but I must be missing something.  What
> have I done wrong?
>
>
> Your code is 100% correct, conceptually. Sadly, it’s suffering from a
> mistake that I saw many many times in the 90s and before. And that’s
> precisely why we get beginners started on Beginning Student Language in
> DrRacket instead of plain Racket. Try it out for your next few exercises or
> even this one, to see whether the error message would have helped.
>
> — Matthias
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Why doesn't this maxval function correctly return the maximum value in the list

2017-02-12 Thread Angus
I am a beginner Racket developer by the way.

Here is my maxval function which is supposed to take a list and return the 
largest integer in the list.

 (define (maxval lst)
   (define (aux lst max)
 (cond [(null? lst)  max]
   [(car lst) > max (aux (cdr lst) (car lst))]
   [#t (aux (cdr lst) max)]))
   (aux lst 0))


Using the function always returns the last value in the list

> (maxval (list 1 2 3 5 6 1))
1

The code looks correct to me, but I must be missing something.  What have I 
done wrong?

Angus

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Contract violation where number expected - can't understand why

2016-10-01 Thread Angus
On Saturday, 1 October 2016 16:13:01 UTC+1, Ben Greenman  wrote:
> Maybe this will help:
> 
> 
> 
> 
> (struct interval (small big guesses))
> 
> creates 5 new functions
> interval, for making intervals. For example (interval 5 10 0) creates an 
> interval from 5 to 10 that has 0 guessesinterval?, for testing if a value is 
> an interval. For example (interval? 1) is #false and (interval? (interval 0 0 
> 0)) is #trueinterval-small, to get the value of the "small" field out of an 
> interval. For example, (interval-small (interval 8 9 10)) is 8interval-big, 
> to get the value of the "big" fieldinterval-guesses, to get the value of the 
> "guesses" field
> This is all described in the Racket docs for struct
> http://docs.racket-lang.org/reference/define-struct.html
> 
> 
> 
> 
> 
> The value of (+ (interval-guesses w) 1) is a number 1 greater than the value 
> of the "guesses" field in the interval w.
> Notice that you give this value as the 3rd argument to a call to (interval 
> )


Thanks for the responses.  My confusion was with the meaning of w.  I see now 
that functions such as on-tick etc return the current state of the world.  In 
my program case the type is interval.  So on-key returns a world state, ie an 
interval.

And the bigger function takes arguments w which is the current world state.  It 
is confusing for me I think because in C (and C like languages) the function 
would be defined as:

WorldState bigger(WorldState previous_state)

The types are less explicit in lisp.

And it is now more obvious to me that in:

(define (bigger w)
  (interval (min (interval-big w) 
 (add1 (guess w)))
(interval-big w) (+ (interval-guesses w) 1)))

The (interval... part is a constructor ? for an interval and that is what this 
function returns - the new state of the world.

It's interesting here that there is no assignment as such.  Although big-bang 
must somehow be dealing with mutating internal state somehow.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Contract violation where number expected - can't understand why

2016-10-01 Thread Angus
On Saturday, 1 October 2016 15:30:19 UTC+1, Vincent St-Amour  wrote:
> On Sat, 01 Oct 2016 09:13:25 -0500,
> Angus wrote:
> > Then I changed smaller (and bigger) like this:
> > 
> > (define (smaller w)
> >   (interval (interval-small w)
> > (max (interval-small w) 
> >  (sub1 (guess w)
> > 
> > to
> > 
> > (define (smaller w)
> >   (interval (interval-small w)
> > (max (interval-small w) 
> >  (sub1 (guess w))) ((+ interval-guesses 1) w)))
> > 
> > 
> > 
> > But with these changes, when I run the program I get:
> > 
> > +: contract violation
> >   expected: number?
> >   given: #
> >   argument position: 1st
> >   other arguments...:
> >1
> 
> What is `interval-guesses`? Your code would like it to be a number, but
> it's actually an *accessor*. If you pass it an interval, then you'll get
> the number you want.
> 
> Jumping ahead a little: you may also want to double-check how you're
> constructing your intervals.
> 
> Vincent

This seems to work:

(define (smaller w)
  (interval (interval-small w)
(max (interval-small w) 
 (sub1 (guess w))) (+ (interval-guesses w) 1)))

But I don't understand why???

the first argument is passed as (interval w)

what does that mean?  Does it mean that interval-small should be set to the 
current interval-small???

So then what does 

(+ (interval-guesses w) 1) mean?

set interval guesses to whatever the current state is (represented by w) plus 1?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Contract violation where number expected - can't understand why

2016-10-01 Thread Angus
On Saturday, 1 October 2016 15:30:19 UTC+1, Vincent St-Amour  wrote:
> On Sat, 01 Oct 2016 09:13:25 -0500,
> Angus wrote:
> > Then I changed smaller (and bigger) like this:
> > 
> > (define (smaller w)
> >   (interval (interval-small w)
> > (max (interval-small w) 
> >  (sub1 (guess w)
> > 
> > to
> > 
> > (define (smaller w)
> >   (interval (interval-small w)
> > (max (interval-small w) 
> >  (sub1 (guess w))) ((+ interval-guesses 1) w)))
> > 
> > 
> > 
> > But with these changes, when I run the program I get:
> > 
> > +: contract violation
> >   expected: number?
> >   given: #
> >   argument position: 1st
> >   other arguments...:
> >1
> 
> What is `interval-guesses`? Your code would like it to be a number, but
> it's actually an *accessor*. If you pass it an interval, then you'll get
> the number you want.
> 
> Jumping ahead a little: you may also want to double-check how you're
> constructing your intervals.
> 
> Vincent

I don't understand.  How would I pass it an interval?

I am confused about the w parameter in smaller.  ie (define (smaller w)

what is w?  Is that the current interval held as state within big-bang?

So in that case do I write it like this:

(interval-guesses (+ w 1))

???

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Contract violation where number expected - can't understand why

2016-10-01 Thread Angus
I am now attempting to do the following ch5 question in Realm of Racket:

Change the Guess My Number game so that it displays the number of guesses the 
program takes to find the players number.  Hint: you might need to change the 
data used to represent the data used to represent the world's state.

So what I did was to extend the interval struct from:

(struct interval (small big)

to

(struct interval (small big guesses)


Then I changed smaller (and bigger) like this:

(define (smaller w)
  (interval (interval-small w)
(max (interval-small w) 
 (sub1 (guess w)

to

(define (smaller w)
  (interval (interval-small w)
(max (interval-small w) 
 (sub1 (guess w))) ((+ interval-guesses 1) w)))



But with these changes, when I run the program I get:

+: contract violation
  expected: number?
  given: #
  argument position: 1st
  other arguments...:
   1

In the Racket debugger it is complaining at the line (+ interval-guesses 1) 

But surely, (+ interval-guesses 1) when evaluated would return a number???

Why am I getting this error?  and how would I fix it?


Here is my full code for the program:

#lang racket
(require 2htdp/universe 2htdp/image)

(struct interval (small big guesses) #:transparent)


(define TEXT-SIZE 10)

(define HEIGHT 150)
(define COLOR "red")
(define SIZE 72)
(define TEXT-X 3)
(define TEXT-UPPER-Y 10)
(define TEXT-LOWER-Y 135)

(define HELP-TEXT
  (text "up arrow for larger numbers, down arrow for smaller ones"
TEXT-SIZE
"blue"))

(define HELP-TEXT2
  (text "Press = when your number is guessed; q for quit."
TEXT-SIZE
"blue"))

(define WIDTH (+ (image-width HELP-TEXT2) 10))

(define MT-SC
  (place-image/align
   HELP-TEXT TEXT-X TEXT-UPPER-Y "left" "top"
   (place-image/align
HELP-TEXT2 TEXT-X TEXT-LOWER-Y "left" "bottom"
(empty-scene WIDTH HEIGHT

(define (guess w)
  (quotient (+ (interval-small w) (interval-big w)) 2))

(define (render w)
  (overlay (text (number->string (guess w)) SIZE COLOR) MT-SC))

(define (smaller w)
  (interval (interval-small w)
(max (interval-small w) 
 (sub1 (guess w))) ((+ interval-guesses 1) w)))

(define (bigger w)
  (interval (min (interval-big w) 
 (add1 (guess w)))
(interval-big w) ((+ interval-guesses 1) w)))

(define (deal-with-guess w key)
  (cond [(key=? key "up") (bigger w)]
[(key=? key "down") (smaller w)]
[(key=? key "q") (stop-with w)]
[(key=? key "=") (stop-with w)]
[else w]))

(define (single? w)
  (= (interval-small w) (interval-big w)))

(define (start lower upper)
  (big-bang (interval lower upper 0)
(on-key deal-with-guess)
(to-draw render)
(stop-when single? render)))


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Realm of Racket chapter 5 easy question variable update question

2016-10-01 Thread Angus
I am working through Realm of Racket chapter 5, last exercise was: Find an 
image of a locomotive and create an animation which wraps around to the left 
side of the screen after passing the right margin.

I have a solution which is below, but have some questions:

1. Is the solution below a reasonable one?  How could I have done it better?

2. I don't really understand what is happening with the big-bang on-tick.  The 
big-bang function is passed 50 as the initial state.  Does the big-bang 
function somehow internally keep the state variable cached and then it gets 
updated by the change-state function.  That bit seems a little confusing to me. 
 Or should I just read more of the book until it is clearer?


My background is imperative languages, eg C, C++, Java, python, etc.  Not sure 
if that helps.

My solution to problem:

#lang racket
(require 2htdp/universe 2htdp/image)

(define LOCO-IMAGE )

(define WIDTH 800)
(define HEIGHT 200)

(define (change-state current-state)
  (cond
[(>= current-state 750) 0]
[else (+ current-state 3)]))

(define (draw-a-ufo-onto-an-empty-scene current-state)
  (place-image LOCO-IMAGE current-state (/ HEIGHT 2)
   (empty-scene WIDTH HEIGHT)))

(big-bang 50 
  (on-tick change-state)
  (to-draw draw-a-ufo-onto-an-empty-scene))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] difference between function returning a string and function returning a symbol

2016-09-24 Thread Angus
When you say it is an expression not a function, what do you mean?  Are you 
saying that if the symbols represented something then it would make sense?  I 
am a bit lost on your reply?



On Saturday, September 24, 2016 at 5:10:31 PM UTC+1, Matthias Felleisen wrote:
> For the sheer pleasure of playing with symbols. (It is an expression not a 
> function.) 
> 
> 
> > On Sep 24, 2016, at 11:45 AM, Angus wrote:
> > 
> > I am reading through Realm of Racket and saw this function in the 
> > conditions chapter:
> > 
> > (if (= (+ 1 2) 3)
> >  'yup
> >  'nope)
> > 
> > So this function returns a symbol.  Will be 'yup in above case.  But 'yup 
> > confuses me.  I don't really understand why you would want to return 'yup.
> > 
> > For example, I could re-write the function like this:
> > 
> > (if (= (+ 1 2) 3)
> >  "yup"
> >  "nope")
> > 
> > which now returns a string.  This is more understandable, it is simpler.
> > 
> > Why would you want to write a function like the first one which returns 
> > 'yup?
> > 


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] difference between function returning a string and function returning a symbol

2016-09-24 Thread Angus
I am reading through Realm of Racket and saw this function in the conditions 
chapter:

(if (= (+ 1 2) 3)
  'yup
  'nope)

So this function returns a symbol.  Will be 'yup in above case.  But 'yup 
confuses me.  I don't really understand why you would want to return 'yup.

For example, I could re-write the function like this:

(if (= (+ 1 2) 3)
  "yup"
  "nope")

which now returns a string.  This is more understandable, it is simpler.

Why would you want to write a function like the first one which returns 'yup?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Assigning a new value to a global variable in Rachet - how?

2016-09-17 Thread Angus
I am reading Chapter 2 Creating your first lisp program in the Land of Lisp 
book and attempting to use the code in Racket.  The syntax is slightly 
different and the smaller function is defined in Land of Lisp as:

(defun smaller ()
  (setf *big* (1- (guess-my-number)))

where *big* is a global variable.

In Racket I defined the other vars/functions like this:
(define *small* 1)
(define *big* 100)
(define (guess-my-number) 
  (arithmetic-shift (+ *small* *big*) -1))


My attempts at defining smaller in Rachet (which didn't work):

(define (smaller)
  (define *big* (- (guess-my-number) 1)))

(define (smaller)
  (set *big* (- (guess-my-number) 1)))

How can I translate this function into Rachet syntax?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.