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)
Related
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.
I am using Clojure to do the following task -
Write a function named get-divisors which takes a number n as input and returns the all the numbers between 2 and √𝑛 inclusive
I have this code so far, that seems to be working as expected:
(defn get-divisors [n]
(str (range 2 (Math/sqrt n))))
The user inserts and input and the code shall display all numbers between 2 and the square root of that particular number. (I know! get-divisors is a horrible name for the function)
I type (get-divisors 101) I get the following output
"(2 3 4 5 6 7 8 9 10)" which is correct.
However, the issue is when I use the number 4 I get a result of nil or () when I should in-fact get 2. Or when I enter 49 I should get all numbers between 2 and 7 but I only get all the numbers between 2 and 6.
I have searched online for some information. I am new to Clojure however, the information on this programming seems to be scarce as opposed to the likes of Java, JavaScript. I have read another thread which was based on a similar situation to mind, however, the suggestions/answers didn't work for me unfortunately.
I would appreciate any help. Thank you.
Please see the Clojure CheatSheet. range does not include the upper bound. So, in general, you probably want something like
(range 2 (inc n))
or in your case
(range 2 (inc (Math/floor (Math/sqrt n))))
Also check out http://clojure.org
I start to read/work on clojure and for that I start to read in parallel 'Programming Clojure' and 'Practical Clojure' books. I saw there one example of how lazy sequence working and for me was very clear in order to understand how lazy-seq work but unfortunately it doesn't work or at least not how I expect.
here is the example:
(defn square[x]
(do
(println "[current.elem=" x "]")
(* x x))
)
(def var-00 (map square '(1 2 3 5 6 4)))
when I call:
var-00
, I expect that no message to print on console(REPL) but I got the follow result:
([current.elem= 1 ][current.elem= 2 ]1 [current.elem= 3 ]4 [current.elem= 5 ]9 [current.elem= 6 ]25 [current.elem= 4 ]36 16)
this mean that the function map was called even I expect to nothing happen since 'var-00' is just a reference to function 'map'; and more awkward from my point of view, if I call:
(nth var-00 2)
I got:
[current.elem= 1 ][current.elem= 2 ][current.elem= 3 ]9
, and if I call again:
(nth var-00 3)
I got:
[current.elem= 1 ][current.elem= 2 ][current.elem= 3 ][current.elem= 5 ]25;
previous elements(1,2,3) was computed again I my opinion those elements should be 'cached' by first call and now only element 5 should be computed. Did I do something wrong or I didn't fully understand how lazy sequence working in clojure ? As a mention I use IntellijIDEA and LaClojure plugin to run the program.
Thanks Sorin.
Just checked your coed in Clojure REPL, it works fine for me. Every element got printed only once (when it's evaluated the first time).
I even tried your example in Clojure online REPL:
But there is one thing that you got wrong. REPL executes each command and then prints its results, so when you type var-00 REPL resolves the symbol and then, in order to print it, executes the whole lazy sequence:
It have nothing to do with lazy sequences, it's just how REPL works:
Lazy Evaluation dosen't mean that things will be cached. It means that inside a calculation an element will only be evaluated, if it is needed for the result. If an element is needed twice for the result, it might be evalueated twice.
If you want to have automatic caching of elements there is the memoize function, which will return a transformed version of the input function with added caching of results. This is also a easy way to implement dynamic programming
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))
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.