"format" command - how to solve it pls

Discussion of Common Lisp
Post Reply
freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

"format" command - how to solve it pls

Post by freezy39 » Thu Mar 18, 2010 1:40 pm

Hi guys,

could you help me a little bit pls?

I would like to make a frame with parameters x y f , x and y means volume, f means function.
The frame should be made from "#" and interior should be also made by "#" but it depends on predictate/function f.

For example:

Code: Select all

(make-pic 5 8 (lambda (m n) nil))
########
#      #
#      #
#      #
########
NIL

Code: Select all

(make-pic 4 4 #’<)
####
# ##
#  #
####
NIL
Here is my code:

Code: Select all

(defun make-pic (a b function)
(dotimes (x b)
  (format T "#"))
  (format T "~%")

(dotimes (x (- a 2))
 (format T "#")
(dotimes (x (- b 2))

  (format T "a")      /////////here should be some function, which will print the correct output

     )
 (format T "#")
 (format T "~%"))

(dotimes (x b)     
  (format T "#")))

Code: Select all

Output
##########
#aaaaaaaa#
#aaaaaaaa#
##########
NIL

guys, any ideas please?

Kohath
Posts: 61
Joined: Mon Jul 07, 2008 8:06 pm
Location: Toowoomba, Queensland, Australia
Contact:

Re: "format" command - how to solve it pls

Post by Kohath » Thu Mar 18, 2010 11:04 pm

Hey freezy39, first thing, please put your code and results into a

Code: Select all

[code]your lisp code here
[/code] format so that it's easier to read.

You'll probably want to use the format control ~C, which lets you change your (format t "a") into:

Code: Select all

(format t "~C" (funcall function x y))
But just make sure that your function returns a character, like #\\d, or #\\# (this is literal representation of a single character in Lisp). If you want to just have two characters and the function makes the other one get printed, then you could do:

Code: Select all

(format t "~C" (if (funcall function x y) #\- #\#))
or even cooler :):

Code: Select all

(format t "~:[#~;-~]" (funcall function x y))

freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

Re: "format" command - how to solve it pls

Post by freezy39 » Fri Mar 19, 2010 11:34 am

thank you man, I will try it and let you know .

freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

Re: "format" command - how to solve it pls

Post by freezy39 » Fri Mar 19, 2010 1:15 pm

* Edit post
* Delete post
* Report this post
* Reply with quote

Re: "format" command - how to solve it pls

Postby freezy39 » Fri Mar 19, 2010 1:13 pm

Code: Select all

(defun make-pic (a b function)
  (dotimes (x b)
    (format T "#"))
  (format T "~%")
  (dotimes (x (- a 2))
    (format T "#")
    (dotimes (x (- b 2))
      (format t "~C" (if (funcall function a b) #\- #\#)))
    (format T "#")
    (format T "~%"))
  (dotimes (x b)
    (format T "#")))

its drawing whole picture but also with inside . when I will put there some function, it seems that it hasnt any dependency.

Code: Select all

CL-USER 28 > (make-pic 3 10 #'>)
##########
##########
##########
NIL
grrrrrrr.
should be like this

Code: Select all

CL-USER 28 > (make-pic 3 10 #'>)
# ########
#  #######
#   ######
NIL
_ - means space

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

Re: "format" command - how to solve it pls

Post by gugamilare » Fri Mar 19, 2010 8:58 pm

If you don't mind, I took the liberty to edit your posts to make them a little more readable.

The problem is that here

Code: Select all

(format t "~C" (if (funcall function a b) #\- #\#)))
you are comparing the given arguments, a and b, not the current position in the picture. For instance, in:

Code: Select all

(make-pic 3 10 #'>)
it will always make a call equivalent to this one:

Code: Select all

(format t "~C" (if (funcall #> 3 10) #\- #\#)))
and (> 3 10) always returns nil (false), so it always prints the #.

So, you should use different variables to each loop. Instead of using x twice, use x and y. Like this:

Code: Select all

  (dotimes (x (- a 2))
    ...
    (dotimes (y (- b 2))
       ...))
Then substitute a and b with x and y in (funcall function a b).

One last thing, just to clarify: comments in Lisp begin with ';' and not '//'

freezy39
Posts: 7
Joined: Thu Mar 18, 2010 1:24 pm

Re: "format" command - how to solve it pls

Post by freezy39 » Mon Mar 22, 2010 10:35 am

hey guys, thank you so much. Its working right now. You are right, I used different variables to each loop and its working.

the last question is, how to do that via recursion ??

(defun make-pic (num1 num2 fun)
(unless (= num1 0) (make-pic (- num1 1) num2 fun)
(if (funcall fun num1 num2) (format T "#")
(format T " ")))
(unless (= num2 0) (make-pic num1 (- num2 1) fun )
(if (funcall fun num1 num2) (format T "#")
(format T " "))) (format T "~%"))

but seems to be not working properly, any idea pls ?


CL-USER 2 > (make-pic 3 7 #'<)

#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#



#

#








#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#



#

#








#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#



#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#


=======================
or:

CL-USER 3 > (make-pic 3 7 (lambda (x y) nil))








NIL

CL-USER 4 >

Post Reply