Reading in Integers to a list

Discussion of Common Lisp
Mercfh
Posts: 17
Joined: Tue Mar 30, 2010 3:39 pm
Location: Miami,FL

Re: Reading in Integers to a list

Post by Mercfh » Tue Apr 06, 2010 6:09 pm

Ok so it'd be something like

Code: Select all

defun readin()
(let ((list     ;; <- this is the variable created, its value will be the next form
       (loop for value = (prompt "Please enter an integer or \"end\" to finish:")
          until (eq value 'end)
          collect value)))
))
Btw thanks a ton. u've been a HUGE help

and then I can do something like

Code: Select all

(defun testing ()
(let ((test nil))
(readin())
(dolist (el *list*)
(insertf test el)
(print (frob-tree test)))
So it reads them all into a list first?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Reading in Integers to a list

Post by gugamilare » Tue Apr 06, 2010 6:49 pm

Mercfh wrote:Ok so it'd be something like

Code: Select all

defun readin()
(let ((list     ;; <- this is the variable created, its value will be the next form
       (loop for value = (prompt "Please enter an integer or \"end\" to finish:")
          until (eq value 'end)
          collect value)))
))
Btw thanks a ton. u've been a HUGE help

and then I can do something like

Code: Select all

(defun testing ()
(let ((test nil))
(readin())
(dolist (el *list*)
(insertf test el)
(print (frob-tree test)))
So it reads them all into a list first?
No. let will declare a local variable, and the extent of that local variable finishes where the let finishes. In your example, the variable list will only exist inside the function readin.

What you were supposed to do is this:

Code: Select all

(defun readin ()
  (loop for value = (prompt "Please enter an integer or \"end\" to finish:")
     until (eq value 'end)
     collect value))
Then you create a variable in the function that calls this function:

Code: Select all

(defun your-function ()
  (let ((list (readin)))
    (dolist (element list)
      (do-something-with element))))

Mercfh
Posts: 17
Joined: Tue Mar 30, 2010 3:39 pm
Location: Miami,FL

Re: Reading in Integers to a list

Post by Mercfh » Tue Apr 06, 2010 9:03 pm

Ah Ok i think I understand.

Although our Professor emailed us because people were asking.

APPARENTLY i was just supposed to read in all the values into a list and then do the other functions. instead of what I initially thought (reading in an integer 1 by 1, then performing the task on it....1 by 1)

Although I think I understand now. I can just read them all into a list. but is there any way to kinda store the list as a "global variable"?
since before I had this as an example list

Code: Select all

(defparameter *example-list*
(list 4 6 4 18 8 2 14 7 15 5 19 12 15 5 9 0 17 2 2 19))
then I did this number on it

Code: Select all

(defun testing ()
(let ((test nil))
(dolist (ms *example-list*)
(insertf test ms)
(print (print-tree test)))
Or is the list you made using ur code already a global variable? so i can just replace *example-list* with it.
IM sorry for being such a newb with these things, but this is a basically learn on my own kinda thing sadly. (our teacher has been ill for the past 2 weeks, sadly no extension on our hw tho)

EITHER WAY, using ur code I get this error:
*** - EVAL: undefined function Ttree::PROMPT
ttree is the name of my package.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Reading in Integers to a list

Post by nuntius » Tue Apr 06, 2010 9:57 pm

Re the global variable: Just (setf *example-list* new-value) -- though why not pass the list as a parameter?

Re the undefined function: Did you copy his prompt function into your package and compile it first?

Mercfh
Posts: 17
Joined: Tue Mar 30, 2010 3:39 pm
Location: Miami,FL

Re: Reading in Integers to a list

Post by Mercfh » Tue Apr 06, 2010 10:10 pm

nuntius wrote:Re the global variable: Just (setf *example-list* new-value) -- though why not pass the list as a parameter?

Re the undefined function: Did you copy his prompt function into your package and compile it first?
Ya I put it in my package and ran (load "CS.lisp")
and it said *** - EVAL: undefined function PROMPT

I even just made it's OWN file like this

Code: Select all

(defpackage :test2
    (:use :common-lisp))


(let ((list (loop for value = (prompt "Please enter an integer or \"end\" to finish:")
                  until (eq value 'end)
                  collect value)))
  (format t "This is the list constructed: ~A~%" list)
  (format t "These are its elements:")
  (dolist (element list)
    (format t "~A" element))
  (format t "~%"))
and I still got that problem.


And I understand what you mean by the global variable. but I need a way to read the list INTO that global variable.
like:
Read data into list called *mylist*
then I can use that global variable on the other functions (which already work)
because here is my function that works perfectly (for my ternary tree)

Code: Select all

(defun testing()
  (let ((test nil))
    (dolist (hi *mylist*)
      (insertf test hi)
      (print (list-tree test)))))
im just using this right now as an example list

Code: Select all

(defparameter *example-list*
(list 4 6 4 18 8 2 14 7 15 5 19 12 15 5 9 0 17 2 2 19))

I need a way to read in numbers INTO that *example-list* or another global variable like it. if that makes sense? or if thats even possible

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Reading in Integers to a list

Post by gugamilare » Wed Apr 07, 2010 5:10 am

Mercfh wrote:
nuntius wrote:Re the global variable: Just (setf *example-list* new-value) -- though why not pass the list as a parameter?

Re the undefined function: Did you copy his prompt function into your package and compile it first?
Ya I put it in my package and ran (load "CS.lisp")
and it said *** - EVAL: undefined function PROMPT

I even just made it's OWN file like this

Code: Select all

(defpackage :test2
    (:use :common-lisp))


(let ((list (loop for value = (prompt "Please enter an integer or \"end\" to finish:")
                  until (eq value 'end)
                  collect value)))
  (format t "This is the list constructed: ~A~%" list)
  (format t "These are its elements:")
  (dolist (element list)
    (format t "~A" element))
  (format t "~%"))
and I still got that problem.
You forgot to copy the function prompt in my early posts.
Mercfh wrote:[...]

I need a way to read in numbers INTO that *example-list* or another global variable like it. if that makes sense? or if thats even possible
That is what he explained to you.

Se also viewtopic.php?f=2&t=683&p=4263#p4263

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Reading in Integers to a list

Post by Jasper » Wed Apr 07, 2010 6:31 am

Don't forget (in-package the-package) then after that the things you declare in that file are actually in said page.

Post Reply