Scheme language: #void error output - list

This is my program:
(define (listof n m)
(display " ( ")
(let loop ((times n))
(if (= times 0)
(display ") ")
(begin (display m)
(display " ")
(loop (- times 1))))))
=> (listof 2 (listof 2 2))
Expected output: ((2 2) (2 2)) .
Output received: ( 2 2 ) ( # < void> # < void> )
How can I fix this?

The result of the listof function is #< void >.
This is because the last thing that is done is (display ") ").
This prints ") " to standard output, but doesn't return a meaningful value to the calling function (hence #< void >). This means that you can't nest calls to listof in this way.
So in other words, to fix this, you have to make the function return a meaningful value. You could construct a string with string-append for example. I'm assuming you don't want to construct actual lists here, otherwise you should look into how lists work.
Here's an example (this one only works with strings):
(define (listof n m)
(let loop ((times n) (res "("))
(if (= times 0)
(string-append res " )")
(loop (- times 1) (string-append res " " m)))))
> (listof 3 (listof 2 "hello"))
"( ( hello hello ) ( hello hello ) ( hello hello ) )"

Since (listof 2 (listof 2 2)) i supposed to output ((2 2) (2 2)) I'm pretty sure you must make code that produces the list and it has nothing to do with printing. Thus you cannot use display.
Why? Well. Imagine if (listof 2 2) should print "(2 2)" it is obvious that the return is no consequence. Thus the same output as display is ok. Thus (listof 2 (listof 2 2)) should display (2 2) (? ?) where ? is whatever what listof returns.
If listof should return a list with the passed argument then (listof 2 2) should return (cons 2 (cons 2 '()). You need to rewrite your procedure such that it does that instead.

Related

how to make a recursive racket list that, from the input list, outputs its decreasing to 1 for every element in the list (e.g. ('3 2) outputs ('32121)

****What I tried****
(define(help num)
(if(= num 1)
num
(cons(num (help( - num 1))))))
;i called this defination in the bottom one
(define (list-expand L)
(cond
[(empty? L)'()]
[(=(car L)1)(cons(car L)(list-expand (cdr L)))]
[(>(car L)1) (cons(help(car L)(list-expand(cdr L))))]))
In the help procedure, the base case is incorrect - if the output is a list then you must return a list. And in the recursive step, num is not a procedure, so it must not be surrounded by brackets:
(define (help num)
(if (<= num 0)
'()
(cons num (help (- num 1)))))
And in list-expand, both recursive steps are incorrect. You just need to test whether the list is empty or not, calling help with the correct number of parameters; use append to combine the results, because we're concatenating sublists together:
(define (list-expand L)
(if (empty? L)
'()
(append (help (car L)) (list-expand (cdr L)))))
That should work as expected, but please spend some time studying Scheme's syntax, you still have trouble with the basics, for instance, when and where to use brackets...
(list-expand '(3 2))
=> '(3 2 1 2 1)
Just for fun - a non-recursive solution in Racket:
(append-map (lambda (n) (stream->list (in-range n 0 -1))) '(3 2))
;; or:
(append-map (lambda (n) (for/list ((x (in-range n 0 -1))) x)) '(3 2))
Returning:
'(3 2 1 2 1)

getting undefined reference to a function in commonLISP

I'm trying to compile the following code:
(defun nextStates (st)
"generate all possible states"
(setf N 0)
(setf states-list (make-list 9))
(setf list-actions (possible-actions))
(loop for x in list-actions do
(setf aux (nextState st x))
(when (not(member aux (states-list) :test #'equalp))
(progn
(setf (nth N states-list) aux)
(setf N (+ N 1))
)
)
)
(values states-list)
)
nextState is a function and states-list is a list, both of which are defined. I'm getting "undefined reference to states-list".
I don't know what I'm doing wrong.
Any help will be greatly appreciated
Renaming the badly camel case to lisp case I'm left with this.
(defun next-states (st)
"generate all possible states"
(loop :for action :in (possible-actions)
:for aux := (next-state st action) :then (next-state st action)
:when (not (member aux states-list :test #'equalp))
:collect aux :into states-list
:finally (return states-list)))
As a test here is what I had:
(defun possible-actions ()
"have duplicates to see not member in action"
'(0 1 3 3 4))
(defun next-state (st action)
"Simple version for test"
(+ st action))
(next-states 3) ; ==> (3 4 6 7)

Concatenating list elements - Scheme

If i have a scheme code that generates the following result: (i'm using cons)
'((1 . 0) . 0)
How can i take this, and just simply display 100 as if it were just one integer number and not a list presented with those dots and parenthesis?
Thanks!
EDIT:
my full code:
(define (func X)
(if ( <= X 3 )
X
(cons (modulo X 4) (func(floor(/ X 4)) ))
))
If I understand correctly, you're trying to convert a number from base 10 to base 4, and then display it as a number, but there are several problems with your implementation.
You're building a list as output - but that's not what you want, you want a number. Also, you're traversing the input in the wrong order, and that's not the correct way to find the quotient between two numbers. Perhaps this will help:
(define (func X)
(let loop ((n X) (acc 0) (mult 1))
(if (< n 4)
(+ (* mult n) acc)
(loop (quotient n 4)
(+ (* mult (modulo n 4)) acc)
(* mult 10)))))
Alternatively, you could output a string to stress the fact that the output is not in base 10:
(define (func X)
(let loop ((n X) (acc ""))
(if (< n 4)
(string-append (number->string n) acc)
(loop (quotient n 4)
(string-append (number->string (modulo n 4)) acc)))))
It'll work as expected:
(func 16)
=> 100
Oscar Lopez's answer is excellent. I can't help adding that this problem doesn't need the "loop" construct:
;; translate a string to a base-4 string.
(define (func n)
(cond [(< n 4) (number->string n)]
[else (string-append (func (quotient n 4))
(number->string (modulo n 4)))]))

Typed racket, changing a single component of a list

Is there a way to change a single value of a list, called by list-ref, to a different value in typed Racket?
Like: (Change (list-ref (list 1 2 2) 0) 4)
Would output: (list 4 2 2)
(: duck : (Listof Integer) -> (Listof Integer))
(define (duck n)
(match n
[' () ' ()]
[(cons x r)
(cond
[(= x (list-ref n 1))
(cons 4 (duck r))]
[else (cons x (duck r))])]))
I wrote this recursive function in an attempt to do so, but it doesn't work. My logic is as follows: If x is (list-ref n 1) then change x to 4.
Any help is much appreciated.
list-set from unstable/list does what you want:
#lang typed/racket/base
(require/typed unstable/list
[list-set (All (a) (Listof a) Natural a -> (Listof a))])
(list-set (list 1 2 2) 0 4)
; '(4 2 2)
Update:
The latest version of racket now provides list-set from typed/racket:
#lang typed/racket
(list-set (list 1 2 2) 0 4)
Or if you wanted to use typed/racket/base:
#lang typed/racket/base
(require racket/list)
(list-set (list 1 2 2) 0 4)

How to return a list of numbers between 1 and x

I'm trying to create a function that takes in user input, x, and displays all of the numbers from 1 up to x. Not quite sure where to go from here:
(define (iota x)
(if (>= x 1)
(display
;; Takes a number n
;; and returns a list with 1..n
;; in order.
(define (make-list n)
(let loop ((n n) (accumulator '()))
(if (zero? n)
accumulator
(loop (- n 1) (cons n accumulator)))))
;; prints data and adds
;; a newline
(define (println x)
(display x)
(newline))
;; prints 1..n by applying println
;; for-each element over the list 1..n
(define (iota n)
(for-each println (make-list n)))
Basically you want to use recursion. So think of it was counting up. As you count up either you've added enough numbers and you are done building your list or you need to add the current number to your list and go on to the next number.
Look at the following code:
(define (range-2 next-num max-num cur-list)
;;if we've added enough numbers return
(if (> next-num max-num)
cur-list
;; otherwise add the current number to the list and go onto the next one
(range-2
(+ 1 next-num)
max-num
(append cur-list (list next-num))
)))
But to get the function you wanted you need to start off with the constants you specified (ie 1), so lets create a convenience function for calling range with the starter values you need to set up the recursion, sometimes called priming the recursion:
(define (range X)
(range-2 1 X `() ))
You could do it without the second function using lambda, but this is pretty common style from what I've seen.
Once you've constructed a list of the numbers you need you just display it using
(display (range 10))
If you're using Racket you're in luck, you can use iterations and comprehensions, for instance in-range:
(define (iota x)
(for ([i (in-range 1 (add1 x))])
(printf "~a " i)))
Or for a more standard solution using explicit recursion - this should work in most interpreters:
(define (iota x)
(cond ((positive? x)
(iota (- x 1))
(display x)
(display " "))))
Either way, it works as expected:
(iota 10)
=> 1 2 3 4 5 6 7 8 9 10
(define iota2
(lambda (y)
(let loop ((n 1))
(if (<= n y)
(cons n (loop (+ n 1)))
'()))))
(define (iota x)
(if (>= x 1)
(begin
(iota (- x 1))
(display x)
(newline))))