Computer Science Canada Scheme TYS |
Author: | Hikaru79 [ Sat Nov 18, 2006 1:25 pm ] | ||||
Post subject: | Scheme TYS | ||||
I'm sure you all know the drill by now I'll get things started! DISCLAIMER: This TYS, and probably a few others that I'll put up, are shamelessly stolen from my own CS assignments (AFTER the assignment has been handed in, of course ). I can't help it, some of these questions are genuinely fun. PROBLEM 1: A directed graph can be represented as a list of nodes, where each node is a list consisting of a symbol (the node's name) and a list of vertices to its neighbors. For example, the following is a valid graph:
And it would look like this: Note that this is a directed graph, so the vertices have a direction; also, the graph may or may not contain cycles (this particular example doesn't, but that's no guarantee). Write a function that takes a graph and reverses all of its vertices. (ie, if A->B before, now B->A). For example, reverse-ing the graph we showed before should give
Which is basically the diagram shown except with all the arrows drawn in the opposite direction. |
Author: | Cervantes [ Sat Nov 18, 2006 3:39 pm ] | ||
Post subject: | |||
**shamelessly steals his answer to this question without doing any additional work**
Sorry, it's not proper syntax for full scheme. I'd convert it to work in full scheme, but I foldr didn't appear to work. I need some good scheme documentation. |
Author: | wtd [ Sat Nov 18, 2006 3:42 pm ] | ||
Post subject: | |||
|
Author: | Hikaru79 [ Sat Nov 18, 2006 3:59 pm ] | ||||
Post subject: | |||||
My approach is similar to both of yours'; in fact, almost identical to Cervantes' though I opted for local-define'd functions instead of nested lambda's. In Dr.Scheme-Scheme:
And here it is in Standard Scheme:
|
Author: | wtd [ Sun Nov 19, 2006 2:11 pm ] | ||
Post subject: | |||
I'm going to submit a mild variation:
|
Author: | Lazy [ Mon Nov 20, 2006 5:26 am ] | ||
Post subject: | |||
A Haskell solution, worked fine with your test data. It assumes that all nodes are given on the left-hand side, so it'll work with [(A,[B]), (B, [])] but not [(A, [B])]. Node Ids go only to G cos I'm a lazy typist. It would probably be better to use Ints or Chars anyway...
wtd, is that OCaml you're using? I'm looking for a good comparison of Haskell and OCaml, but my Google Fu is weak... |
Author: | wtd [ Mon Nov 20, 2006 9:02 am ] |
Post subject: | |
Actually, in this case it's SML. |
Author: | Lazy [ Wed Nov 22, 2006 5:52 am ] |
Post subject: | |
Any more assignments? |
Author: | Hikaru79 [ Sun Dec 03, 2006 7:51 am ] |
Post subject: | |
Lazy wrote: Any more assignments?
In retrospect, most of the assignment questions tend to be a) Too easy for a TYS b) Too formulaic for a TYS (anyone can look up a graph traversal or implementation of some abstract list function) c) Too long However, I have an exam coming up on the 9th, and textbook material tends to get very dry very fast, so if you or wtd want to put up some interesting Scheme challenges, they'd be good preparation |
Author: | Hikaru79 [ Thu Dec 07, 2006 1:03 pm ] | ||||
Post subject: | |||||
Alright, here's another interesting one Write a function
that returns a list representing the shortest path from the word "start" to the word "end" with each step in the path being a word in "dictionary" which is exactly one character different from the previous word. For example, a valid path from "house" to "march" might be
There's a rather long list of around 8,000 words that you can use as a wordlist for testing purposes available here I know this falls rather on the long side for a TYS, though it is possible to do it in around 20 lines. Good luck! (I'll post my solution after a few others have posted) |
Author: | Cervantes [ Thu Dec 07, 2006 2:28 pm ] | ||
Post subject: | |||
20 lines? Pshaah! Here's my solution, using BST's. I would like to clean up the one-aways function.
|
Author: | Hikaru79 [ Sun Feb 04, 2007 2:29 am ] | ||||||
Post subject: | Re: Scheme TYS | ||||||
Here is another one One can represent a polynomial as a list of lists, with each element being an ordered pair representing a coefficient and a power of x. For example,
Write a function that will take a polynomial in the given form, and represent it in its sparse canonical form; that is, there will be exactly one entry for each non-zero coefficient, in ascending order of degree. For example, the above polynomial has a sparse canonical representation of
NOTE: If a particular power's coefficient becomes 0 during the simplification, it should not be included in the representation. |
Author: | wtd [ Sun Feb 04, 2007 9:50 am ] | ||
Post subject: | RE:Scheme TYS | ||
Because everyone loves reductions...
|
Author: | Hikaru79 [ Sun Feb 04, 2007 10:25 am ] | ||
Post subject: | Re: Scheme TYS | ||
Interesting solution, wtd However...
Coefficients of 0 should drop out; after all, we would never write "0x^3" as a polynomial. The result here should have been the empty list. |
Author: | Hikaru79 [ Sun Feb 04, 2007 10:46 am ] | ||||
Post subject: | Re: Scheme TYS | ||||
Oh, and here's my solution, translated to standard R5RS Scheme. It LOOKS overly long and complicated, but my dirty secret is that remove-duplicates was actually a question on an earlier assignment, which I copied over word-for-word, so the amount of new code I wrote here is actually pretty small (And actually, a lot of Scheme implementations have their own remove-duplicates, so this is not even neccesary.) Two helper functions and then one nice big long convoluted abstract list function.
Then we have:
Note: Hmm, someone should modify the Scheme syntax highlighting file so it doesn't highlight the "list" keyword unless it's got whitespace on both sides... |
Author: | Cervantes [ Sun Feb 04, 2007 1:27 pm ] | ||
Post subject: | Re: Scheme TYS | ||
Here's mine, not translated to R5RS.
|
Author: | wtd [ Mon Feb 05, 2007 11:36 pm ] | ||||
Post subject: | RE:Scheme TYS | ||||
New one. Probably rather simple, but let's see what kind of responses we get. Write a tail-recursive function split which splits a list into a pair of lists, based on whether or not the members satisfy a predicate.
Should result in:
|
Author: | Cervantes [ Tue Feb 06, 2007 3:08 pm ] | ||
Post subject: | RE:Scheme TYS | ||
Seems like pretty easy accumulative recursion:
This function is useful in implementing a quicksort. |
Author: | wtd [ Tue Feb 06, 2007 5:06 pm ] | ||
Post subject: | RE:Scheme TYS | ||
My own solution implemented this in terms of a left -> right reduction. An O'Caml version:
|