| | |
scheme newbie here!!!
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
![]() |
•
•
Join Date: Apr 2005
Posts: 1
Reputation:
Solved Threads: 0
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.
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
A lambda always looks the same way:
So a function that, say, adds two numbers looks like:
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:
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:
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
The list was not empty three times.
Our function may look like this:
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:
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:
At this point, the remaining list is length zero, so we just return the currently-known size:
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:
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:
(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:
•
•
•
•
If I can do something to the car of a list, then I can do that something to the whole list.
'() (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)() emptyThe list was not empty three times.
Our function may look like this:
Scheme Syntax (Toggle Plain Text)
(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 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:
4When 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))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.
•
•
Join Date: Nov 2007
Posts: 30
Reputation:
Solved Threads: 6
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/
My first year cs course at university was in scheme, hes the free text we used.
http://www.htdp.org/
•
•
Join Date: Oct 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
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.
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Scheme Newbie (Legacy and Other Languages)
- PHP newbie, project feasibility (PHP)
- Dr Scheme example (Computer Science)
- How to network two Win98 machines (Networking Hardware Configuration)
- Change Out Your Pointer Scheme (Windows tips 'n' tweaks)
Other Threads in the Computer Science Forum
- Previous Thread: Need help! model design and estimation
- Next Thread: My own programming language
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignmenthelp assignments automata battery bigbrother binary bittorrent bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect conversion csc dataanalysis dataintepretation development dfa dissertation dissertations dissertationthesis dissertationtopic ebook employment energy floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone itcontracts jobs kindle laser linkbait lsmeans mainframes marketing mobileapplication msaccess nano netbeans networking news os p2p piracy piratebay principles programming rasterizer research sam-being-cute sas science security sex simulation software spying stephenfry study supercomputer supercomputing sweden technology textfield turing turingtest two'scompliment uk virus ww2






