Page 1 of 1
"format" command - how to solve it pls
Posted: Thu Mar 18, 2010 1:40 pm
by freezy39
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?
Re: "format" command - how to solve it pls
Posted: Thu Mar 18, 2010 11:04 pm
by Kohath
Hey freezy39, first thing, please put your code and results into a
[/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))
Re: "format" command - how to solve it pls
Posted: Fri Mar 19, 2010 11:34 am
by freezy39
thank you man, I will try it and let you know .
Re: "format" command - how to solve it pls
Posted: Fri Mar 19, 2010 1:15 pm
by freezy39
* 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
Re: "format" command - how to solve it pls
Posted: Fri Mar 19, 2010 8:58 pm
by gugamilare
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:
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 '//'
Re: "format" command - how to solve it pls
Posted: Mon Mar 22, 2010 10:35 am
by freezy39
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 >