Jump to content

Literals in scheme

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Anthony Scottina

Anthony Scottina

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, I need to write a function in Scheme that takes a literal as an argument and returns a complementary literal. For example, if the function were name litcom, then:

(litcom 'a) = (not a)
(litcom '(not a)) = a

This confuses me right off the back, as it seems to me like I would have to define the literal a to be a boolean value of true first.

The function I've written so far is this:

(define litcom(lambda (lit)
(cond ((equal? lit #t) (not lit))
((equal? lit #f) (not lit))
)))

If I define a literal as either true or false, then this function will return the complementary value. For example:

(define a #t)

(litcom a) returns #f


But this isn't what I'm trying to do. What I need to be returned is what like the example at the beginning. I don't understand how:

For any literal _, to return (not _)

And for any literal (not _) return _


Any help will be greatly appreciated, thanks.

#2
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
First you have to remember that not, in most programming language, is boolean operator. No wonder that from the basic point of view it must work with boolean value. However not usually able to work with bits (usually represented by integers). So I believe (depends on the actual language and dialect you are using) that you still can get "not not a = a" if you cast a into bits/integers.

For example:
If a = 01010101(b), then not a = 10101010(b). then not not a = not 10101010(b) = 01010101(b). There you get that "not not a=a" with a does not have to be boolean values only.

#3
Anthony Scottina

Anthony Scottina

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, thanks for the response. I understand now that all the variables I'm using have a boolean value of #t.

What I essentially need to do is traverse a list of boolean values in scheme and make sure that a boolean and its complement don't occur in the list. So if a occurs in a list, then I want to create a function that returns #t if (not a) doesn't occur in the list and #f if it does.

I'm having difficulty doing this because I have to compare the variable name (memory location?) as opposed to the variable value.

For example, if the function litcom returns (not a), and (not a) is a part of my list, I need to be able to compare the two by checking if they are the same variable. This isn't the same as comparing their values. (not a) and (not b) both have the same value of #f, but they aren't both the complementary literal of a.

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
Sorry for late reply, but you confused me here. Perhaps some code sample would clear your intention?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users