How to use Map/Reduce in Scheme? - mapreduce

So I have a homework assignment for my CS course covering Scheme procedures. We just started learning the language last week, so I'm lost on how to answer this. I know the map procedure can get the dot product of two lists, but I didn't think reduce was needed to get the product. Below is example code he gives that reverses a list along with the question on a function convolution and dot product. I am also confused on the equation of the list y and how to actually read it. Any help would be greatly appreciated.
(define (reverse lis)
(if (null? lis)
'()
(append (reverse (cdr lis))
(list (car lis)))))
Sketch a function convolution in Scheme that computes the dot product of a list x with the reverse of a list y:
You may use a map/reduce approach, iterate over the list and add each product using a recursive call, or use any approach you wish.

It's probably way to late, but here it is:
Basically Sigma in the equation can be calculate with reduce (reduce + list-of-values) but to simply sum all the items you don't need reduce because you can use (apply + list-of-values). The map part can be use to calculate the list of numbers from two lists. NOTE that Scheme don't define reduce but you can use:
(fold-left + 0 '(1 2 3))
where 0 is inital value
Map work like this:
You can pass single list:
(map square (list 1 2 3))
But in fact map accept any number of lists as arguments:
(map * (list 1 2 3) (list 3 4 5))
and this will return the list of 3 elements where 1st item in first is list is multiplied with first item in second list. So it's like joining two lists and return single list.
To get proper list as output you simply need to reverse the given lists and pass it as one of the argument with orgiginal list.
And then sum up the resulting list. You should write nice function for that:
(define (dot l)
...)
Now you should be able to write the dot function yourself. Sorry but you will never learn if you will give the code itself. Everything is explained.

Related

Scheme cons won't take two number arguments

I've seen many instances of cons taking two numbers as arguments, and I've been told to pass pass two numbers as arguments into cons in a lab, but whenever I do I get the following error:
> (cons 1 2)
cons: second argument must be a list, but received 1 and 2
If I do the following, I get the same error:
> (cons '1 '2)
cons: second argument must be a list, but received 1 and 2
I'm very new to Scheme, and I don't understand why this is happening.
That's because of the teaching language in use, it's probable that you're working with a student language with certain limitations. To solve the problem, make sure that this line is at the beginning of the file:
#lang racket
And choose the option "Determine language from source" in the bottom-left corner of DrRacket's window. Now this should work as expected:
(cons 1 2)
=> '(1 . 2)

Keep same list after repeated calls to function in Racket

I'm using Racket and what I want to do is develop a random list of given elements that also has a given length. I know how to create the list but the problem that I'm having is I don't know how to keep the same list every time I call the function using the list from the command line without re-creating the list which would be different since the list is consists of randomly chosen elements. This is what I have:
(define gameColors '(red green blue yellow black))
(define currentGameList '())
(define input 4)
(define randomNumber (random 5))
(if (equal? randomNumber 0)
(if (< (length currentGameList) (+ input 1))
(set! currentGameList (append currentGameList (list (car gameColors))))
;;otherwise
(set! currentGameList currentGameList))
;;otherwise
(set! currentGameList currentGameList))
And then the if block repeats for each of the different possible results for randomNumber. All I need to know is how can I call my guess function repeatedly from the command line, which uses currentGameList, without having my program recreate currentGameList every time. The guess function also has parameters that have to be entered by the user so it must be entered at command line each time. Any help is appreciated.
First thing: avoid using set!. In another kind of programming language you'd use mutation of variables for solving the problem, but that's not the way to go in Scheme. The code in the question won't work, nothing on it is iterating or recurring over the color list, and only the first element of gameColors is being picked, every time (there's nothing random about it). If I understood the question (it's a tad confusing), this is what you were aiming for:
(define (generate-random lst len)
(for/list ([x (in-range len)])
(list-ref lst (random (length lst)))))
(generate-random '(red green blue yellow black) 4)
=> '(black black blue green) ; one possible output
Of course if you need to save a particular list generated by one invocation of generate-random (because every time the results will be different), simply store the list in a variable for future use:
(define currentGameList (generate-random '(red green blue yellow black) 4))

get pairs out of a huffman tree

I'm trying to write a procedure Huffman-leaves; the procedure returns a list of pairs from a created huffman tree.
Example on how it runs
(huffman-leaves sample-tree)
->((A . 8) (C . 5) (B . 1) (D . 1))
What I've comed up with but got writers block...
(define (huffman-leaves tree)
(define (huffman-get-pairs current-branch pairs)
(if (or (null? tree) (null? current-branch))
pairs
(let ((next-branch
(get-branch (car current-branch) current-branch)))
(not (member? next-branch pairs)
(if (leaf? next-branch)
(cons (append pairs next-branch)
(display pairs)
(huffman-get-pairs (cadr current-branch) pairs))
(huffman-get-pairs next-branch pairs))))))
(huffman-get-pairs tree '()))
(member? item 'list) #if item in list return #t else #false
I know that I'm doing something wrong but I can't see it.
How can I stop a search in a huffman-tree in scheme? Any tip that I should be doing different?
I recommend:
Write a data definition for Huffman Tree
Make example input huffman trees, encoded according to your data definition from step 1, and expected outputs (lists of leaves, in this case).
Follow the design recipe to build a basic template for the huffman-leaves function.
Fill in the ... in your template according to the examples you built from step 2.
Translate your examples from step 2. into tests, and test your code from step 4.
Sorry if the above seems vague, but it is the best advice I can give with the level of detail (or lack thereof) you have supplied in this question thus far. If you can provide answers for the steps above, and say explicitly which step you are blocked on, then we can help you get over your writers block in a more systematic way.
If you prefer real code, here is one direction you could go in to make a very generic solution for your problem:
;; make-visitor : (X -> Y) (X -> [Listof X]) (Y [Listof Z] -> Z) -> Z
;; Very generic descend+map+recombine routine
;; (note X, Y, Z are implicitly universally quantified above).
(define (make-visitor transform children combine)
(lambda (x)
(let rec ((x x)) ;; rec : X -> Z
(combine (transform x)
(map rec (children x))))))
;; ... the hard bit is coming up with the appropriate lambda
;; expressions for `transform`, `children`, and `combine` above.
(define leaves
(make-visitor (lambda (x) ...)
(lambda (x) ...)
(lambda (y zs) ...)))
I don't actually recommend trying to jump directly to a solution of this form; you will be better off if you try to follow the design recipe and make a direct solution to your problem. But once you have done that, it can be an educational exercise to see if you can retrofit your own solution onto the generic routine above.

Scheme sorting a list

Okay so I am trying to take in a list and sort it from greatest to smallest.
Example:
> (maxheap (list 5 6 2 1 18 7))
;output:
> (18 7 6 5 2 1)
So here's what I got so far:
(define (mkmaxheap heaplist)
(let ((max (mymax(heaplist))))
;mymax is a func that returns max number, it works
(let (( head (car heaplist)) (tail (cdr heaplist)))
(if (null? tail)
newlist))))
Thats all I could get to compile, all the other code I wrote failed. Any help on solving this would be much appreciated.
You should articulate carefully the strategy that you want to use for producing the sorted list. Is it something like this?
Find the maximum number in the list.
Get the rest of the list except for the maximum.
Sort the rest of the list, and put the maximum on the front of it.
This is not a very fast way to sort it, but it should work. The next step from your code will be to write a function to get the rest of the list except for the maximum (take care to handle it correctly if the list has duplicates.)
Once you have that written, you should be able to write Scheme code that looks more or less just like the outline above.
This is a merge sort algorithm in Common lisp. It's roughly close to implementing the same sort in scheme.
(defun merge-sort( input )
(labels ((right-half ( input )
(last input (ceiling (/ (length input) 2))))
(left-half ( input )
(ldiff input (right-half input ))))
(if (or (null input) (null (cdr input)))
input
(merge 'list (merge-sort (left-half input)) (merge-sort (right-half input)) #'<))))
You need to decide what to use to sort the list. I've been tinkering about with scheme recently, working my way through SICP and the "Schemer" series, and I found it pretty easy to implement a bubble sort, merge sort, and quicksort in scheme.
You didn't specify the implementation you're using. But it may implement either r6rs list-sort or srfi-95 sort or any other built-in sorting. Check out your implementation's documentation.

Whats wrong with this Clojure program?

I recently started reading Paul Grahams 'On Lisp', and learning learning clojure along with it, so there's probably some really obvious error in here, but I can't see it: (its a project euler problem, obviously)
(ns net.projecteuler.problem31)
(def paths (ref #{}))
; apply fun to all elements of coll for which pred-fun returns true
(defn apply-if [pred-fun fun coll]
(apply fun (filter pred-fun coll)))
(defn make-combination-counter [coin-values]
(fn recurse
([sum] (recurse sum 0 '()))
([max-sum current-sum coin-path]
(if (= max-sum current-sum)
; if we've recursed to the bottom, add current path to paths
(dosync (ref-set paths (conj #paths (sort coin-path))))
; else go on recursing
(apply-if (fn [x] (<= (+ current-sum x) max-sum))
(fn [x] (recurse max-sum (+ x current-sum) (cons x coin-path)))
coin-values)))))
(def count-currency-combinations (make-combination-counter '(1 2 5 10 20 50 100 200)))
(count-currency-combinations 200)
When I run the last line in the REPL, i get the error:
<#CompilerException java.lang.IllegalArgumentException: Wrong number of args passed to: problem31$eval--25$make-combination-counter--27$recurse--29$fn (NO_SOURCE_FILE:0)>
Apart from the question where the error is, the more interesting question would be: How would one debug this? The error message isn't very helpful, and I haven't found a good way to single-step clojure code, and I can't really ask on stack overflow every time I have a problem.
Three tips that might make your life easier here:
Wrong number of args passed to: problem31$eval--25$make-combination-counter--27$recurse--29$fn (NO_SOURCE_FILE:0)>
Tells you roughly where the error occurred: $fn at the end there means anonymous function and it tells you it was declared inside recurse, which was declared inside make-combination-counter. There are two anonymous functions to choose from.
If you save your source-code in a file and execute it as a script it will give you a full stack trace with the line numbers in the file.
at net.projecteuler.problem31$apply_if__9.invoke(problem31.clj:7)
Note you can also examine the last exception and stack trace from within the REPL by examining *e eg: (.stackTrace *e) The stack trace is at first quite daunting because it throws up all the Java internals. You need to learn to ignore those and just look for the lines that refer to your code. This is pretty easy in your case as they all start with net.projecteuler
You can name your anonymous functions to help more quickly identify them:
(fn check-max [x] (<= (+ current-sum x) max-sum))
In your case using all this info you can see that apply-if is being passed a single argument function as fun. Apply does this (f [1 2 3]) -> (f 1 2 3). From your comment what you want is map. (map f [1 2 3]) -> (list (f 1) (f 2) (f 3)). When I replace apply with map the program seems to work.
Finally, if you want to examine values you might want to look into clojure-contrib.logging which has some helpers to this effect. There is a spy macro which allows you to wrap an expression, it will return exactly the same expression so it does not affect the result of your function but will print out EXPR = VALUE, which can be handy. Also on the group various people have posted full tracing solutions. And there is always the trusty println. But the key skill here is being able to identify precisely what blew up. Once you know that it is usually clear why, but sometimes printouts are needed when you can't tell what the inputs are.
dont have a REPL on me though it looks like:
(defn apply-if [pred-fun fun coll]
(apply fun (filter pred-fun coll)))
takes a list like '(1 2 3 4 5) filters some of them out '(1 3 5)
and then creates a function call like (fun 1 3 5)
and it looks like it is being called (apply-if (fn [x] with a function that wants to receive a list of numbers as a single argument.
you could change the apply-if function to just pass call to the fun (with out the apply) or you could change the call to it to take a function that takes an arbitrary number of arguments.