943,747 Members | Top Members by Rank

Ad:
Apr 12th, 2005
0

scheme newbie here!!!

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ryu12341 is offline Offline
1 posts
since Apr 2005
Nov 22nd, 2007
-1

Re: scheme newbie here!!!

Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jamshid is offline Offline
70 posts
since Jan 2006
Nov 22nd, 2007
0

Re: scheme newbie here!!!

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 anything) in Scheme.

A lambda always looks the same way:
(lambda arg-list body-sexpr)

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:
(define add (lambda (a b) (+ a 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:
Quote ...
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:
Scheme Syntax (Toggle Plain Text)
  1. (define get-list-len (lambda (my-ls current-count)
  2. (if (eq? my-ls '())
  3. current-count
  4. (get-list-len (cdr my-ls) (+ current-count 1))
  5. )
  6. ))
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
Last edited by Duoas; Nov 22nd, 2007 at 7:56 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 23rd, 2007
0

Re: scheme newbie here!!!

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/
Reputation Points: 11
Solved Threads: 6
Light Poster
InfiNate is offline Offline
30 posts
since Nov 2007
Nov 23rd, 2007
0

Re: scheme newbie here!!!

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...
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 5th, 2007
-1

Re: scheme newbie here!!!

Click to Expand / Collapse  Quote originally posted by InfiNate ...
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
Last edited by jumar01; Dec 5th, 2007 at 3:29 am.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
jumar01 is offline Offline
3 posts
since Oct 2007
Dec 5th, 2007
0

Re: scheme newbie here!!!

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
Last edited by Ancient Dragon; Dec 5th, 2007 at 9:37 am. Reason: removed email
Reputation Points: 9
Solved Threads: 0
Newbie Poster
jumar01 is offline Offline
3 posts
since Oct 2007
Dec 5th, 2007
0

Re: scheme newbie here!!!

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Computer Science Forum Timeline: Need help! model design and estimation
Next Thread in Computer Science Forum Timeline: My own programming language





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC