[racket-users] Re: Checking if two lists contain a word

2017-06-29 Thread philipp . thiess1999
Am Sonntag, 25. Juni 2017 19:40:08 UTC+2 schrieb philipp.t...@gmail.com:
> Hello its me again,
> 
> I have the task to make a funcition who checks if two lists contain a word.
> 
> I made a function which does that with one lst but I dont find a way to do it 
> with two lists. There must be some easy trick but I dont find it.
> 
> The function has to look like this: (test2 word list1 list2) and its only 
> allowed to use one function.
> 
> Here is my function for checking one list. Can someone edit it so I can make 
> it with two lists? Would help me alot.
> 
> 
> 
> 
> #lang racket
> 
> 
> (define (test1 word book)
> 
>   (cond ((null? book) '(false))
> 
> ((equal? word (first book)) '(true))
> 
> (else (test1 word (rest book)

Thanks for the advice. I have no more question! :)

-- 
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] Re: Checking if two lists contain a word

2017-06-28 Thread David Storrs
On Wed, Jun 28, 2017 at 3:33 AM,  wrote:

> Thanks guys the tips made me have the idea that I can just write the check
> for the second list instead of '(true).
>
> Here is my solution:
>
> #lang racket
>
>
> (define (test1 word book book2)
>
>   (cond ((null? book) '(false))
>
> ((equal? word (first book)) (cond  ((null? book2) '(false))
>
>((equal? word (first
> book2)) 'true)
>
>(else (test1 word book
> (rest book2)
>
> (else (test1 word (rest book) book2
>
>
> Thanks you again
>

Well done!  Congratulations, you just wrapped your head around recursion.
It can be a brain-breaker, so kudos for figuring it out.

Now that you've gotten yourself over the hump, here's a few comments to
help you with the next one:

First, you're returning '(false) or 'true, which is a bit odd because they
aren't booleans.  Racket has two boolean values, #t and #f.  (They can also
be spelled true and false, but use the #t / #f versions.)  Everything that
isn't #f is a true value.  Note the differences:

#t;; The boolean value for truth.
true ;; Another way to spell #t. (eq? #t true) shows they are
literally the same value in memory.
'true;; A four-character symbol that has nothing to do with boolean
values
#f ;; The boolean value for false.
false ;; Another way to spell #f. (eq? #f false) shows they are
literally the same value in memory.
'false;; A five-character symbol that has nothing to do with
boolean values. **It is a true value!!**
'(false)  ;; Shorthand for (list 'false).  It is a true value.

I generally recommend using #t and #f -- they stand out more, they're more
standard as far as I've seen, and they're fewer keystrokes.

The other reason it's odd to use '(false) and 'true is because they are
different types -- the first is a list containing a symbol, the second is a
symbol.  Typically you would want to use a single datatype as your return
value unless there's a specific reason not to.  In particular, as mentioned
above, '(false) is a true value so it won't work the way you expect if you
do a boolean check on it.

Moving on:  The important part of what you wrote is that you figured out
how to recur through a list by calling the function on (rest book1), and
you even managed to do it for two lists.  Again, serious kudos.  Recurring
through lists is one of the most common things to do in Racket, so of
course there are some functions to help with that.
https://docs.racket-lang.org/reference/pairs.html  The most relevant one
for our purposes is 'member':
https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._member%29%29
which will return #t if a specified element is in a list.  So, for example:

(member 'foo (list 'a 'b 'c 'foo 'bar));; '(foo bar), which is a true
value
(member 'foo (list 'a 'b 'c 'bar))   ;; #f
(member 'foo (list 'a 'b 'c (list 'foo) 'bar));; #f, because member
does not recur into sublists and (list 'foo) is not 'foo

The 'and' function (
https://docs.racket-lang.org/reference/if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29)
will return #f if any of its arguments are false and will return a true
value if they are all true.  (Specifically, it will return the last value
in the collection.)

Your function can therefore be written as follows:

(define (test1 word book1 book2)
  (and (member word book1)
  (member word book2)))

Testing:

(test1 'foo '(bar foo baz) '(2 slag foo))  ;; '(foo), which is a true value
(test1 'foo '(bar foo baz) '(2 slag));;  #f



My suggestion would be to sit down and read through the page on list
functions in the Reference:  (
https://docs.racket-lang.org/reference/pairs.html) so you know what's out
there, then go through the Racket Guide
https://docs.racket-lang.org/guide/index.html in this order:

Sections 1-4
Section 8 (skip 8.4 and 8.5 for now)
Section 11
Everything else in the Guide, in whatever order you like.


Racket is a huge and sprawling language, so whatever it is you want to do
probably has a built-in answer.  The documentation is extremely thorough;
much of it is very approachable, but other parts you will need to read it
and then move on while trusting that things will start to make sense as you
learn more of the language.

Let us know if you have more questions.

Dave


PS:  I have frequently written in to the list with a complex solution in
hand, only to be told 'oh, yeah, here's a built-in function that does
that'.  It can be disheartening, but I try to look at it from the positive
side:  Writing the long version helped me learn the material and taught me
something about why the built-in exists and what its various options are
used for.

-- 
You received this message because you are subscribed to the Google Groups 

[racket-users] Re: Checking if two lists contain a word

2017-06-28 Thread philipp . thiess1999
Thanks guys the tips made me have the idea that I can just write the check for 
the second list instead of '(true).

Here is my solution:

#lang racket


(define (test1 word book book2)

  (cond ((null? book) '(false))

((equal? word (first book)) (cond  ((null? book2) '(false))

   ((equal? word (first book2)) 
'true)
  
   (else (test1 word book (rest 
book2)

(else (test1 word (rest book) book2


Thanks you again

-- 
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] Re: Checking if two lists contain a word

2017-06-26 Thread philipp . thiess1999
Hello I tried another version but it doesnt work. Can anyone help me? Using the 
"and" function didnt help me.


#lang racket


(define (checker wort liste1 liste2)
  (cond ((and (equal? (first liste1) wort)
  (equal? (first liste2) wort))
 '(true))

((or (null? liste1)
 (null? liste2))
 '(false))

((equal? (first liste1) wort) (checker wort (rest liste2) (rest 
liste2)))

((equal? (first liste2) wort) (checker wort (rest liste1) (rest 
liste1)

  

-- 
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] Re: Checking if two lists contain a word

2017-06-25 Thread Vityou
Try taking a look at the `and` function: 
https://docs.racket-lang.org/reference/if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29

On Sunday, June 25, 2017 at 11:40:08 AM UTC-6, philipp.t...@gmail.com wrote:
> Hello its me again,
> 
> I have the task to make a funcition who checks if two lists contain a word.
> 
> I made a function which does that with one lst but I dont find a way to do it 
> with two lists. There must be some easy trick but I dont find it.
> 
> The function has to look like this: (test2 word list1 list2) and its only 
> allowed to use one function.
> 
> Here is my function for checking one list. Can someone edit it so I can make 
> it with two lists? Would help me alot.
> 
> 
> 
> 
> #lang racket
> 
> 
> (define (test1 word book)
> 
>   (cond ((null? book) '(false))
> 
> ((equal? word (first book)) '(true))
> 
> (else (test1 word (rest book)

-- 
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.