Hey guys and gals!!! first time for me posting on this forum!!! I'm currently learning scheme and for some reason I can't seem to get anything to work correctly. I've been trying to define the union of 2 lists in scheme and its not working. I'm not sure how to compare each element in a list and if they are the same, then only outputting one of the elements. for example if i call the function (union '(a b c) '(c d e)) it should return (a b c d e).

heres some code:

(define union
(lambda (A) (D)) -- is this correct for inputting two lists?
(if eq? A(car D) -- is this correct for comparing elements in the two lists?
(return append ((A) (D)) -- is this correct for joining two lists?

any help would be greatly appreciated!! i've been working on this for 3 hours and can't seem to think of anything that would work.

Scheme (and other functional languages) don't look or work the same way as imperative languages like C. There is no such thing as (return [I]anything[/I]) in Scheme.

A lambda always looks the same way: ([B]lambda[/B] [I]arg-list[/I] [I]body-sexpr[/I]) So a function that, say, adds two numbers looks like: (lambda (a b) (+ a b)) It starts with the word "lambda". Next it has a list of argument names. Finally it has an expression that gets evaluated.

You can name things with the "define" function: [B](define add [/B](lambda (a b) (+ a b))[B])[/B] Every expression must always have the proper order.

To get the union of two lists, you need to use some recursion. You will also want to create another function to help.

Everything in Scheme is a list (or more properly, an s-expression). So you have to think in terms of how to do things to a list. The principle is always the same:

If I can do something to the car of a list, then I can do that something to the whole list.

As an example, suppose you want to count the length of a list. There are two cases to worry about: either your list is empty '() (length = 0), or your list is not empty. The length of the list is the number of times it is not empty... (a b c) not empty (1 time). (b c) not empty (2 times) (c) not empty (3 times) () empty
The list was not empty three times.
Our function may look like this:

(define get-list-len (lambda (my-ls current-count)
  (if (eq? my-ls '())
      current-count
      (get-list-len (cdr my-ls) (+ current-count 1))
      )
  ))

Line 1: defines "get-list-length" as a lambda (a function) that takes two arguments: the list to count, and the currently-known size of the list.
Line 2: if the list is empty:
Line 3: then return the currently-known size of the list
Line 4: else return the list length of the cdr of the list using a currently-known length +1

Use it thus: (get-list-length '(a b c d) 0) This says, I want to know the length of '(a b c d), and since I don't know its current size I'll start with zero. Let's trace what happens next: (get-list-length '(b c d) 1) (get-list-length '(c d) 2) (get-list-length '(d) 3) (get-list-length '() 4) At this point, the remaining list is length zero, so we just return the currently-known size: 4 When you write your union function, you will have to go through one of the lists the same exact way: by checking to see if the other list has the first list's car. You'll need another function to do that check (write a lambda that takes a value and a list and returns whether or not the list has that value in it. This is called a predicate). For each car, check to see if the other list has it. If it does, add it to the other list. If not, don't add it to the other list. So you'll need two "if"s in your function:

union A B
  if A is empty
    return B
    if (car A) is in B
      return the union of (cdr A) and B
      return the union of (cdr A) and (B + (car A))

This is obvious pseudo-code. It is as close to an answer as I can dare give you. Now you need to think about why and how this works and, and then you can make Scheme do it.

Hope this helps.

[EDIT] Oh yeah, by "return" I (and your professor) mean that the expression evaluates to a specific value. So to "return" 12, your expression must evaluate to 12. If you haven't already, you should spend some time tracing your expressions as they evaluate: (define diff (lambda (a b) (if (> a b) (- a b) (- b a)))) (diff 7 42) (if (> 7 42) (- 7 42) (- 42 7)) (if #f (- 7 42) (- 42 7)) (- 42 7) 35

Really old thread to bump..

My first year cs course at university was in scheme, hes the free text we used.

http://www.htdp.org/

Yoinks! Your right! That first post was made 2 1/2 years ago!
jamshid, please don't go digging for old posts to reply to...

Really old thread to bump..

My first year cs course at university was in scheme, hes the free text we used.

http://www.htdp.org/

wew..
pls help

pass me a short program containing a output of HELLO WORD using in scheme...thx

pogijumar@yahoo.com

and also a screenshots of the program..

thx more power

commented: no -1

is someone reading my post...
pls help me in using scheme...

containing an simple output of HELLO WORLD,..

just pass to my email to <snipped>

and also a screenshots...
thx godbless

Please stop bumping ancient threads.

No one is going to do your homework for you. You'll have to learn it yourself.
Here is a good place to start.

The very first example is hello world.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.