Oğuzhan Balcı
Oğuzhan Balcı

Reputation: 15

recursive definition of a PROPOSITION

(define( app list1 list2)
  (if(empty? list1) list2
     (cons (car list1) (app(cdr list1)list2))))

(app ((list "↔" "→" "∧" "⊕" "∨" "¬")) (list "P" "Q" "R" "S" "U" "X" "Y" "Z"))




(define L (list "↔" "→" "∧" "⊕" "∨" "¬"))
(define ( f L n)
  (if (= n 0) "p"
      (string-append "p" (car L) (f(cdr L) (- n 1)))))

(f L 3)

You have the following recursive definition of a PROPOSITION:

  1. T, F are propositions ( truth value of propositional variables)
  2. List item
  3. Propositional letters P, Q , R, S, U, X, Y, Z are propositions.
  4. If A is a proposition the ¬A is a proposition.
  5. If A and B are propositions then A⊕B , A→B , A∧B, A∨B , A↔B are propositions.

Write a DrRacket procedure that will randomly generate a proposition with a given number of operations .

I could not complete the function. Can you help me please?

Upvotes: 0

Views: 226

Answers (1)

soegaard
soegaard

Reputation: 31147

Since this is a homework question, I'll show you the techinque using a different example. The following grammar has two non-terminals S and T. For each non-terminal I have defined a function that generates a random string according to the rules. Since S has four rules, I pick one of the rules at random.

#lang racket

;;; Grammar

; The grammar has two non-terminals S and T.
; There are four rules for S and one for T.

; S -> aSa
; S -> bSb
; S -> cT
; S -> ε

; T -> dS

(define (S)
  (case (random 4) ; random number 0,1,2,3 (four rules for S)
    [(0) (string-append "a" (S) "a")]
    [(1) (string-append "b" (S) "b")]
    [(2) (string-append "c" (T))]
    [(3) ""]))

(define (T)
  ; only one rule, so no need for random here
  (string-append "d" (S)))

; generate a random string according to the grammar
(S)

Some example outputs:

"bb"
"bbcdbcdbbb"
"cdbb"

Upvotes: 3

Related Questions