Getting items from a list in Racket - list

I am wondering if in Racket I could get n number of items from a list I've already created. So lets say I made a list in Racket
(define base(list 1 2 3 4 5 6 7 8 9 10))
Now I want to define a function which will pick n number of items from this list and display them in a new list. So lets say n=4 I would want 4 random items from the base list I've made above. An example of the output im looking for would be
'(9 4 3 10)
Is there a way I can do this in Racket?

There are built-in procedures that literally do what you need: shuffling the list and taking n elements from it. Try this:
(define (take-n-random lst n)
(take (shuffle lst) n))
(define base (list 1 2 3 4 5 6 7 8 9 10))
(take-n-random base 4)
=> '(6 9 1 7)

Related

is not of the expected type `REAL' in Common LISP (CL)

I made this function and I want to make it print like this :
(RANGE ‘(0 7 8 2 3 –1))
(-1 8)
;; print 'The range between the smallest number and the second smallest number in the list' and 'The largest number'
So I made code like this :
(defun my-range (list-of-numbers)
(let* ((largest (max list-of-numbers))
(msmallest (min list-of-numbers))
(ssmallest (min (remove (min list-of-numbers) list-of-numbers)))
(range (- msmallest ssmallest)))
(list range largest)))
and I input function and number like the example on debug window :
(my-range '(0 7 8 2 3 -1))
Error: (0 7 8 2 3 -1)' is not of the expected typeREAL'
[condition type: TYPE-ERROR]
The error message was printed in the window...
What is the meaning of 'is not of the expected type 'REAL'?
And how I can fix this problem? And, Could you let me know which problems my code has?
I need your help.

LISP - sequence of integers

Does exist a function in LISP for making a sequence of integers like (0 1 2 3)?
I found make-sequence, but I didn't find out how to make a sequence of integers.
I tried make-list and nothing.
I know that in Scheme exists (build-list 5 (lambda (x) x)). I tried to change the build-list with make-list, but it didn't work.
Some ideas? Thanks
Edit: I need something like make-list 5 ==> (0 1 2 3 4)
Simply done with loop:
(loop :for n :below 10 :collect n)
; ==> (0 1 2 3 4 5 6 7 8 9)
The Alexandria library, which is intended to work on any conforming implementation of Common Lisp, defines iota:
(iota 5)
=> (0 1 2 3 4)
You can also customize start and step:
(iota 3 :start 1 :step 1.0)
=> (1.0 2.0 3.0)
But often you do not need to actually produce the list, you just want to iterate over the given range. That's why there is also map-iota:
(map-iota #'print 3 :start 1 :step 1.0)
=> 3
In such cases you can of course use LOOP:
(loop for i from 1.0 below 22 by 1.5 do (print i))
Instead of do, you can also collect and obtain a list; this is a bit more verbose than iota, but easier to customize.
Lets see if can still write mac lisp of the top of my head:
(defun foo (num acc)
(if (eq num 0)
acc
(foo (- num 1) (cons num acc))))
(foo 5 nil)
should be
(1 2 3 4 5)

Appending list in lisp

In lisp, I am appending lists as:
(setq newlist (append (side a b)(this a b) (that a b) ))
This appends all the required list as: (1 0 0 0 2 0 4 0 6 0)
but what I want is something like this: ((1 0)(0 0)(2 0)(4 0)(6 0))
What should I do to get the required format. Please post code examples in lisp.
So in fact you just need to restructure the elements after you have appended it:
(loop :for (e1 e2)
:on '(1 0 0 0 2 0 4 0 6 0)
:by #'cddr
:collect (list e1 e2))
; ==> ((1 0) (0 0) (2 0) (4 0) (6 0))
Suggested reading is LOOP for black belts, the section you should pay attention to which I've used here is "Looping Over Collections and Packages" and "Destructuring Variables". This is probably the chapter from Practical Common Lisp I read the most. The whole book is very good so every lisper should know about it.

How to make a cumulative sequence?

Say I have a lazy sequence like the following:
(def s (iterate inc 1))
(take 10 s)
=> (1 2 3 4 5 6 7 8 9 10)
Now, I want to generate a sequence of cumulative sum of s like the following:
=> (1 3 6 10 15 ...)
How can I do this?
What I tried is to use atom and accumulate the sum to it(mutating) Is this the only way to generate cumulative sequence or is there a better way to do this?
NOTE: the above cumulative sum is only an example. The source sequence can be other sequence. So I can't use formula: s(n) = n(n+1)/2
(take 10 (reductions + s))
=> (1 3 6 10 15 21 28 36 45 55)

Generate truth table for n operators

I've been tasked with writing a function that generates a table given n operators. The truth table must be in a list and each row of the table must be in separate lists (inside the main list).
I know the solution involves recursion but I just can't seem to think it through.
Can someone help me out? This is only a small part of the assignment.
Easiest way I can think of off the top of my head is to simply convert 2^n to binary and count down, then convert the output to a list.
ie for n=3:
Truth table:
a b c
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
2^3 = 8, 8 in binary = 1000, start from 1000-1 = 111 and work your way down to 0, record outputs, and voila!
If hkf's interpretation of your question is right, this should work in Racket:
#lang racket
(define (generate-table n)
(if (zero? n)
'(())
(for*/list ((y (in-list (generate-table (sub1 n))))
(x (in-list '(0 1))))
(cons x y))))
Use it like this:
(generate-table 3)
> ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1))
Let's assume that all N operators are binary functions, like AND and OR.
;; Common Lisp
(defun truth-tables (ops)
(loop for op in ops
collecting
(loop for args in '((nil nil) (nil t) (t nil) (t t))
collecting (eval `(,op ,#args)))))
(truth-tables '(and or xor)) -> ((NIL NIL NIL T) (NIL T T T) (NIL T T NIL))
This gives you an idea. Well, here I don't have "each row of the truth table" as a sublist; I have the columns for the AND, OR and XOR truth tables, respectively. The input variable combinations are left implicit: you know that the third entry of every one corresponds to (<op> t nil). Your description of the problem is not very clear.
As you can also see, I cheated by using the Lisp operators through generated code which is dynamically evaluated.