[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

HTDP2e Introduction Book

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 4

File: images.jpg (7KB, 225x225px) Image search: [Google]
images.jpg
7KB, 225x225px
Hello today /g/ents

HTDP2e fag back again, still on the DrRacket journey learning the basics.

Yesterday we had a good brainstorm to solve the simple exercises, coming straight from the boolean exercises from chapter 1.5 and 1.6
(http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html)

Trying to solve the following exercise using something other than the "if" boolean because I feel like its trying to get me to incorporate "and" and "or" but am currently failing to find documentation that adequately explains how to use them:

>Create an expression that computes whether a picture is "tall", "wide", or "square".

Currently our tools are number arthmitic primitives and <, >, <=, >=, =, or, and, not, if, image-height/width, shapes etc.

Here is my shitty frankenstein solution since I cant figure out how "and" "or" "not" would work for the exercise:

(define rectangle1 (rectangle 50 50 "solid" "black"))


(if (> (image-height rectangle1) (image-width rectangle1))
"tall" (if (< (image-height rectangle1) (image-width rectangle1))
"wide" (if (= (image-height rectangle1) (image-width rectangle1))
"square" "wtf?")))
>>
"if" statements make perfect sense to me
>if (boolean expression) is true then (first expression possibility) otherwise (2nd expression possibility)

Ok sure, cool.

however, the explanation for "or" from the book:
>or checks whether any of the given Boolean values is #true:
> (or #true #true)
#true
> (or #true #false)
#true
> (or #false #true)
#true
> (or #false #false)
#false

Doesnt really make sense to me. It feels like "or" is used in the middle of an already established expression. Otherwise, what functions can be developed from it?

IE:
(or (= "blue" "blue" "blue"))


That would just print "true"...but I dont see how to incorporate it into anything?
>>
>>56029665
With that said, what I can seem to do is use "and" and "or" to compare two or more expressions being equal or not, which has its value, but how would I use that information to compare if a shape was either tall, wide or square as the exercise suggests?

Again, I can only seem to make that comparison by nesting "if" statements.
>>
(define rectangle1 (rectangle 50 50 "solid" "black"))
(define w (image-width rectangle1))
(define h (image-height rectangle1))

(or (and (< w h) "tall") (and (> w h) "wide") (and (= w h) "square"))
>>
>>56029897
>(define rectangle1 (rectangle 50 50 "solid" "black"))
>(define w (image-width rectangle1))
>(define h (image-height rectangle1))
>(or (and (< w h) "tall") (and (> w h) "wide") (and (= w h) "square"))

>and: question result is not true or false: "square"
>>
>>56029962
Sorry, I didn't test with Racket. It works with Guile and BiwaScheme though.
>>
>>56029897
I am an idiot for not just defining image-width/height like that to begin with, so thanks for that slap of awareness to my shittyness

I just dont see how those are going to print a result other than "true" or "false". the exercise isnt asking for a boolean its asking to print if its either tall, wide or square.
>>
>>56029998
In Guile and BiwaScheme, 'and' returns the last argument that's not #t, and 'or' returns the first argument that's not #f. So, for example, if (< w h) is true, then (and (< w h) "tall" returns "tall", and the full expression also returns "tall".
>>
File: yoda.jpg (18KB, 408x408px) Image search: [Google]
yoda.jpg
18KB, 408x408px
>>56030069
See now that's what I would expect to happen, but Dr racket doesn't have any result printed from "and" and "or" except #true or #false so how would you ever use them in the example in the OP?

Or Are you just not supposed to use anything except "if"?
>>
File: 1278162069202.jpg (77KB, 500x351px) Image search: [Google]
1278162069202.jpg
77KB, 500x351px
Still willing to see if htdp2e describes how to use Dr rackets "and" "or" "not" statements to choose expressions to print or if it's only accomplished through "if" statements.

Please refer to op.
>>
The only way I can see how to use and or statements to make a decision is to nest them in if statements as one of the two expression choices... However this still only allows you print a true or false statement for any use of them.
>>
>>56029897
So, to piggy back off your code, heres the next iteration of the exercise:

>Create an expression that computes whether a picture is "tall", "wide", or "square". image

(define rectangle1 (rectangle 50 40 "solid" "black"))
(define w (image-width rectangle1))
(define h (image-height rectangle1))

(if (> w h) "wide" (if (< w h) "tall" (if (= w h) "square" "wtf?")))


Again, this is only using "if" operators. The chapter mentions "and" "if" "or" "not" staetments but has no exercises that use them. Is this because those can only print true or false statements and it just doesnt apply to the exercise? Or is there some way I am not seeing to use them to help complete the exercise
>>
Still at it, boys, still just as lost. Thanks for riding with me this long. Gonna keep trying to wrap my brain around it.
>>
Just messing around at this point, still failing to try and incorporate "and" "or" or "not" statements to solve the example:

(define rectangle1 (rectangle 60 50 "solid" "black"))
(define w (image-width rectangle1))
(define h (image-height rectangle1))
(define wider (> w h))
(define taller (< w h))
(define squarer (= w h))


(define tallwideorsquare (if squarer "square" (if taller "tall" (if wider "wide" "0"))))


tallwideorsquare
>>
Is there a way to make "and" "or" "not" statements print expressions like "if" does?
>>
File: 1468958504424.jpg (842KB, 795x5898px) Image search: [Google]
1468958504424.jpg
842KB, 795x5898px
This anon still here. Any input on how to solve the OPs exercise by incorporating "and" "or" and "not" statements?
Thread posts: 16
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.