i seem to have some trouble with the following code:
(define-struct speise (vegan name))
(define (vegan? speise1 speise2 speise3)
(cond
[(and (equal? speise1 true)
(and (equal? speise2 true)
(equal? speise3 true))) true]
[else false]))
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise false "Mettkipferl") (make-speise true "Chilli-Spekulatius")) #false)
(check-expect (vegan? (make-speise false "Kuerbis-Fleisch-Suppe") (make-speise false "Mettkipferl") (make-speise false "Chilli-Fleisch-Spekulatius")) #false)
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise true "Salatkipferl") (make-speise true "Chilli-Spekulatius")) #true)
The first 2 tests are fine, but the third one results in an exception, because the actual value differs from the expected one.
I just want to check all 3 items - if all 3 are true, then i just want the programm to print true. If one of these 3 is false, it's supposed to print false.
Sorry that the variables are in german though.
That would be
(define-struct speise (vegan name))
(define (vegan? speise1 speise2 speise3)
(and (speise-vegan speise1)
(speise-vegan speise2)
(speise-vegan speise3)))
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise false "Mettkipferl") (make-speise true "Chilli-Spekulatius")) #false)
(check-expect (vegan? (make-speise false "Kuerbis-Fleisch-Suppe") (make-speise false "Mettkipferl") (make-speise false "Chilli-Fleisch-Spekulatius")) #false)
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise true "Salatkipferl") (make-speise true "Chilli-Spekulatius")) #true)
(check-expect (vegan? (make-speise true "Something") (make-speise false "Somethingelse") (make-speise true "idontknow")) #false)
executing:
Welcome to DrRacket, version 6.10.1 [3m].
Language: Beginning Student; memory limit: 128 MB.
All 4 tests passed!
>
Related
I am going to design an interactive weekly exercise calendar. The program displays the current day and associated exercise, as allows the user to scroll forward/backward in the week by pressing the right/left arrow keys, respectively. When I finished coding, there are some problems in the to-draw and on-key part.
(require 2htdp/image)
(require 2htdp/universe)
(define SUNDAY "Sunday Climbing")
(define MONDAY "Monday Cardio")
(define TUESDAY "Tuesday Upper body+Core")
(define WEDNESDAY "Wednesday Cardio")
(define THURSDAY "Thursday Lower Body + Core")
(define FRIDAY "Friday Cardio")
(define SATURDAY "Saturday Rest")
(check-expect (exercise SUNDAY) "Sunday Climbing")
(check-expect (exercise MONDAY) "Monday Cardio")
(define (exercise e-day)
(cond
[(string=? e-day SUNDAY) SUNDAY]
[(string=? e-day MONDAY) MONDAY]
[(string=? e-day TUESDAY) TUESDAY]
[(string=? e-day WEDNESDAY) WEDNESDAY]
[(string=? e-day THURSDAY) THURSDAY]
[(string=? e-day FRIDAY) FRIDAY]
[(string=? e-day SATURDAY) SATURDAY]))
Next week function:
(check-expect (next-weekday SUNDAY) MONDAY)
(check-expect (next-weekday MONDAY) TUESDAY)
(define (next-weekday d)
(cond
[(string=? d SUNDAY) MONDAY]
[(string=? d MONDAY) TUESDAY]
[(string=? d TUESDAY) WEDNESDAY]
[(string=? d WEDNESDAY) THURSDAY]
[(string=? d THURSDAY) FRIDAY]
[(string=? d FRIDAY) SATURDAY]
[(string=? d SATURDAY) SUNDAY]))
Previous week function:
(check-expect (prev-weekday SUNDAY) SATURDAY)
(check-expect (prev-weekday MONDAY) SUNDAY)
(define (prev-weekday d)
(cond
[(string=? d SUNDAY) SATURDAY]
[(string=? d MONDAY) SUNDAY]
[(string=? d TUESDAY) MONDAY]
[(string=? d WEDNESDAY) TUESDAY]
[(string=? d THURSDAY) WEDNESDAY]
[(string=? d FRIDAY) THURSDAY]
[(string=? d SATURDAY) FRIDAY]))
big-bang:
(define (exercise-calendar initial-d)
(big-bang initial-d
[to-draw draw-day]
[on-key move-day]))
(define BACKGROUND (square 200 "solid" "white"))
(define WEEKDAY(text (exercise e-day) 36 "blue"))
(check-expect
(draw-day day)
(place-image
WEEKDAY
50 50
BACKGROUND))
(define
(draw-day day)
(place-image
WEEKDAY
50 50
BACKGROUND))
(define (move-day p ke)
(cond
[(key=? ke "left") (prev-weekday p)]
[(key=? ke "right") (next-weekday p)]
[else p]))
Just some notes:
This part (define WEEKDAY (text (exercise e-day) 36 "blue")) can't be evaluated, because value of e-day isn't known. You can rewrite it as function with argument e-day, or move it directly into draw-day function.
Don't repeat string=? in next-weekday and prev-weekday. You can use some data structure and fetch data with assoc.
If those check-expect tests are given to you by teacher, you should mark them somehow like this "I have to use this code and can't change it and I have to pass exactly these tests". I didn't find note like this, so I rewrote them to suit my solution.
Rest of the code is similar:
(require 2htdp/image)
(require 2htdp/universe)
(define (exercise day)
(second (assoc day (list (list "Sunday" "Sunday Climbing")
(list "Monday" "Monday Cardio")
(list "Tuesday" "Tuesday Upper body+Core")
(list "Wednesday" "Wednesday Cardio")
(list "Thursday" "Thursday Lower Body + Core")
(list "Friday" "Friday Cardio")
(list "Saturday" "Saturday Rest")))))
(define (next-weekday day)
(second (assoc day (list (list "Sunday" "Monday")
(list "Monday" "Tuesday")
(list "Tuesday" "Wednesday")
(list "Wednesday" "Thursday")
(list "Thursday" "Friday")
(list "Friday" "Saturday")
(list "Saturday" "Sunday")))))
(define (prev-weekday day)
(second (assoc day (list (list "Sunday" "Saturday")
(list "Monday" "Sunday")
(list "Tuesday" "Monday")
(list "Wednesday" "Tuesday")
(list "Thursday" "Wednesday")
(list "Friday" "Thursday")
(list "Saturday" "Friday")))))
(check-expect (exercise "Sunday") "Sunday Climbing")
(check-expect (exercise "Monday") "Monday Cardio")
(check-expect (next-weekday "Sunday") "Monday")
(check-expect (next-weekday "Monday") "Tuesday")
(check-expect (prev-weekday "Sunday") "Saturday")
(check-expect (prev-weekday "Monday") "Sunday")
(define bg (square 500 "solid" "white"))
(define (draw-day day)
(place-image (text (exercise day) 18 "blue") 250 50 bg))
(define (move-day day key)
(cond
[(key=? key "left") (prev-weekday day)]
[(key=? key "right") (next-weekday day)]
[else day]))
(define (exercise-calendar day)
(big-bang day
[to-draw draw-day]
[on-key move-day]))
Start with (exercise-calendar "Monday") and press left and right key.
I am trying to make a function that returns true or false if a list is in order. I can't figure out how to make it recursive. I keep getting error messages on the last line: define: expected only one expression for the function body, but found 1 extra part
(check-expect (is-sorted? (list)) true)
(check-expect (is-sorted? (list "A")) true)
(check-expect (is-sorted? (list "B" "A")) false)
(check-expect (is-sorted? (list "A" "B")) true)
(check-expect (is-sorted? (list "A" "C" "B")) false)
(check-expect (is-sorted? (list "A" "B" "C")) true)
(define (is-sorted? lst)
(cond
((empty-list? lst) true)
((= (length lst) 1) true) ;return true if there is only one element.
((string<? (first lst) second lst) true) ;If the first element is smaller than the second
;return true.
(else (is-sorted? (rest lst))))) ;do the above steps on the rest of the elements in the list.
Notice that you're not considering the case when the procedure should return false, and that you exit prematurely when you find that an element is sorted with respect to the next element (you need to keep iterating! it's just one match, what about the others?). The solution is simple, just negate the condition for this case and ask if it's not sorted return false. Like this:
(define empty-list? empty?)
(define (is-sorted? lst)
(cond
((empty-list? lst) true)
((empty-list? (rest lst)) true)
((string>=? (first lst) (second lst)) false)
(else (is-sorted? (rest lst)))))
It works with your test cases:
(is-sorted? (list)) ; true
(is-sorted? (list "A")) ; true
(is-sorted? (list "B" "A")) ; false
(is-sorted? (list "A" "B")) ; true
(is-sorted? (list "A" "C" "B")) ; false
(is-sorted? (list "A" "B" "C")) ; true
I am stuck on this question. In fact, the code keeps running and doesn't end. Any suggestions?
(check-expect (all-elements? even? (list 1 2 3)) false)
(check-expect (all-elements? even? (list 2 4 6)) true)
(check-expect (all-elements? odd? (list 1 3 5)) true)
(define (all-elements? predicate lst)
(cond
[(empty? lst) false]
[(predicate (first lst)) true]
[else (all-elements? predicate lst)]
)
)
(define (all-elements? predicate lst)
(cond ((empty? lst) true)
((predicate (first lst)) (all-elements? predicate (cdr lst)))
(else false)))
Different approach:
;; make `and` as a function
(define (my-and a b) (and a b))
(define (all-elements? predicate args)
(foldr my-and #t (map predicate args)))
(all-elements? odd? '(1 3 5))
;; #t
or as a variadic function:
(define (all-elements? predicate . args)
(foldr my-and (map predicate args)))
then one can type:
(all-elements? odd? 1 3 5 7 9) ;; #t
i have a problem. How do I get all possible combinations of x booleans in Racket? (on a low language level)
I need something like that:
For x=1
(list
(list false)
(list true))
For x=2
(list
(list false false)
(list false true)
(list true false)
(list true true))
For x=3
(list
(list false false false)
(list false false true)
(list false true false)
(list false true true)
(list true false false)
(list true false true)
(list true true false)
(list true true true))
etc.
I have no idea how to do this in Racket.
Thanks you!
You're asking for all n-size permutations (not combinations!) of the list '(#t #f), with repetitions allowed. Adapting this answer for Racket:
(define (permutations size elements)
(if (zero? size)
'(())
(append-map (lambda (p)
(map (lambda (e)
(cons e p))
elements))
(permutations (sub1 size) elements))))
Use it like this:
(permutations 3 '(#t #f))
=> '((#t #t #t) (#f #t #t) (#t #f #t) (#f #f #t)
(#t #t #f) (#f #t #f) (#t #f #f) (#f #f #f))
Here is one way to convert a number to a list of booleans.
To generate all combinations, use it in a loop as you described.
(map (λ (x) (eqv? x #\1))
(string->list (number->string 12345 2)))
Replace 12345 with any number.
What you're actually doing is sort of building the powerset for a set of size x.
A powerset is the set of all possible subsets. For example, the powerset of (list 1 2 3) is (list (list 1 2 3) (list 1 2) (list 1 3) (list 1) (list 2 3) (list 2) (list 3) empty).
(A set is a subset of itself and the empty set is a subset of all sets.)
Why what you're doing describes the powerset is because an element can either be or not be in a subset. So apply (list true true true) to (list 1 2 3) will return (list 1 2 3) and (list false true true) will return (list 2 3).
This is my code for your problem.
(define baselist (list (list true) (list false)))
;; List1 List2 -> List of Lists
;; Where List1 is any list of lists, and list2 is a list of lists of size 2
;; and all of the lists within list 2 has one element
(define (list-combination list-n list-two)
(cond [(empty? list-n) empty]
[else (cons (append (first list-n) (first list-two))
(cons (append (first list-n) (second list-two))
(list-combination (rest list-n) list-two)))]))
;; tflist Number -> List of Boolean Lists
;; creates baselistn
(define (tflist n)
(cond [(= 1 n) baselist]
[else (list-combination (tlist (sub1 n)) baselist)]))
So (tflist 3) will return your original problem.
Now to make a powerset, you can do the following...
;; subset List1 ListofBooleans -> List
;; Determines which elements of a set to create a subset of
;; List1 and LoB are of the same length
(define (subset set t-f-list)
(cond [(empty? t-f-list) empty]
[(first t-f-list) (cons (first set) (subset (rest set) (rest t-f-list)))]
[else (subset (rest set) (rest t-f-list))]))
;;powerset set -> Powerset
;; produces a powerset of a set
(define (powerset set)
(local ((define upperbound (expt 2 (length set)))
(define tflist (tlist (length set)))
(define (powerset set n)
(cond [(= n upperbound) empty]
[else (cons (subset set (list-ref tflist n)) (powerset set (add1 n)))])))
(powerset set 0)))
I'm very new with functional programming, lisp and lambda calculus. Im trying to implement the AND operator with Common Lisp Lambda Calc style.
From Wikipedia:
AND := λp.λq.p q p
So far this is my code:
(defvar TRUE #'(lambda(x)#'(lambda(y)x)))
(defvar FALSE #'(lambda(x)#'(lambda(y)y)))
(defun OPAND (p q)
#'(lambda(f)
#'(lambda(p) #'(lambda(q) (funcall p (funcall q(funcall p))))))
)
I found this 2 conversion functions:
(defun church2int(numchurch)
(funcall (funcall numchurch #'(lambda (x) (+ x 1))) 0)
)
(defun int2church(n)
(cond
((= n 0) #'(lambda(f) #'(lambda(x)x)))
(t #'(lambda(f) #'(lambda(x) (funcall f
(funcall(funcall(int2church (- n 1))f)x))))))
)
If I do:
(church2int FALSE)
I've got 0. If I do this:
(church2int TRUE)
I have
#<FUNCTION :LAMBDA (X) (+ X 1)>
Which I think it's ok. But if I do this:
(church2int (OPAND FALSE FALSE))
I've got:
#<FUNCTION :LAMBDA (Q) (FUNCALL P (FUNCALL Q (FUNCALL P)))>
Where I should have 0. Is there something wrong with my code? Or am I missing something?
Thanks
If you want to define opand as a function with 2 parameters, like you are trying to, you need to do this:
(defun OPAND (p q)
(funcall (funcall p q) p) )
and then:
(opand false false)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) Y)> ;; which is FALSE
(opand true true)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) X)> ;; which is TRUE
This is my implementation, based on the original paper http://www.utdallas.edu/~gupta/courses/apl/lambda.pdf, of the and operator λxy.xyF
(defvar OPAND
#'(lambda(x)
#'(lambda(y)
(funcall (funcall x y) FALSE) ) ) )
And if you do
(funcall (funcall opand false) false)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) Y)> ;; which is FALSE
(funcall (funcall opand true) true)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) X)> ;; which is TRUE