[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Functional Programming

gengyangcai

9/27/2015 9:10:00 PM


CL-USER 1 > (defun bad-reverse (lst) (let* ((len (length lst))
(ilimit (truncate (/ len 2)))) (do ((i 0 (1+ i))
(j (1- len) (1- j))) ((>= i ilimit))
(rotatef (nth i lst) (nth j lst)))))
BAD-REVERSE

CL-USER 2 > (setq lst '(a b c))
(A B C)

CL-USER 3 > (bad-reverse lst)
NIL

CL-USER 4 > lst
(C B A)

CL-USER 5 > (setq lst '(a b c))
(A B C)

CL-USER 6 > (good-reverse lst)
(C B A)

CL-USER 7 > lst
(A B C)

CL-USER 8 > (defun good-reverse (lst) (labels ((rev (lst acc)
(if (null lst) acc
(rev (cdr lst) (cons (car lst) acc))))) (rev lst nil)))
GOOD-REVERSE

CL-USER 9 > (reverse lst)
(C B A)

CL-USER 10 > (setq lst (reverse lst))
(C B A)

CL-USER 11 > (nreverse lst)
(A B C)

CL-USER 12 > (setq lst '(a b c))
(A B C)

CL-USER 13 > (nreverse lst)
(C B A)

CL-USER 14 > lst
(A)

CL-USER 15 > (nconc x y)

Error: The variable X is unbound.
1 (continue) Try evaluating X again.
2 Return the value of :X instead.
3 Specify a value to use this time instead of evaluating X.
4 Specify a value to set X to.
5 (abort) Return to level 0.
6 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 16 : 1 > (setq x (nconc x y))

Error: The variable X is unbound.
1 (continue) Try evaluating X again.
2 Return the value of :X instead.
3 Specify a value to use this time instead of evaluating X.
4 Specify a value to set X to.
5 (abort) Return to level 1.
6 Return to debug level 1.
7 Return to level 0.
8 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 17 : 2 > (truncate 26.21875)
26
0.21875

CL-USER 18 : 2 > (= (truncate 26.21875) 26)
T

CL-USER 19 : 2 > (multiple-value-bind (int frac) (truncate 26.21875) (list int frac))
(26 0.21875)

CL-USER 20 : 2 > (defun powers (x)
(values x (sqrt x) (expt x 2)))
POWERS

CL-USER 21 : 2 > (multiple-value-bind (base root square) (powers 4)
(list base root square))
(4 2.0 16)

CL-USER 22 : 2 >
5 Answers

Pascal J. Bourguignon

9/27/2015 9:30:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> CL-USER 1 > (defun bad-reverse (lst) (let* ((len (length lst))
> (ilimit (truncate (/ len 2)))) (do ((i 0 (1+ i))
> (j (1- len) (1- j))) ((>= i ilimit))
> (rotatef (nth i lst) (nth j lst)))))
> BAD-REVERSE
>
> CL-USER 2 > (setq lst '(a b c))
> (A B C)
>
> CL-USER 3 > (bad-reverse lst)
> NIL
>
> CL-USER 4 > lst
> (C B A)
> [â?¦]


cl-user> (defun tr (n p)
(unless (zerop p)
(tr (1+ n) (1- p))
(format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
tr
cl-user> (tr 0 4)
*
***
*****
*******
nil
cl-user> (tr 'hello 0)
nil
cl-user> (tr 'hello 1)
> Debug: The value hello is not of the expected type number.
> While executing: (:internal swank::invoke-default-debugger), in process repl-thread(14).
> Type :POP to abort, :R for a list of available restarts.
> Type :? for other options.
1 > :q
; Evaluation aborted on #<type-error #x30200A694DAD>.
cl-user>

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

gengyangcai

9/27/2015 9:42:00 PM

0

CL-USER 1 > (defun tr (n p)
(unless (zerop p)
(tr (1+ n) (1- p))
(format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
TR

CL-USER 2 > (tr 0 4)
*
***
*****
*******
NIL

CL-USER 3 > (tr 'hello 0)
NIL

CL-USER 4 > (tr 'hello 1)

Error: In 1+ of (HELLO) arguments should be of type NUMBER.
1 (continue) Return a value to use.
2 Supply a new argument.
3 (abort) Return to level 0.
4 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.


On Monday, September 28, 2015 at 5:29:48 AM UTC+8, informatimago wrote:
> CAI GENGYANG <gengyangcai@gmail.com> writes:
>
> > CL-USER 1 > (defun bad-reverse (lst) (let* ((len (length lst))
> > (ilimit (truncate (/ len 2)))) (do ((i 0 (1+ i))
> > (j (1- len) (1- j))) ((>= i ilimit))
> > (rotatef (nth i lst) (nth j lst)))))
> > BAD-REVERSE
> >
> > CL-USER 2 > (setq lst '(a b c))
> > (A B C)
> >
> > CL-USER 3 > (bad-reverse lst)
> > NIL
> >
> > CL-USER 4 > lst
> > (C B A)
> > [...]
>
>
> cl-user> (defun tr (n p)
> (unless (zerop p)
> (tr (1+ n) (1- p))
> (format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
> tr
> cl-user> (tr 0 4)
> *
> ***
> *****
> *******
> nil
> cl-user> (tr 'hello 0)
> nil
> cl-user> (tr 'hello 1)
> > Debug: The value hello is not of the expected type number.
> > While executing: (:internal swank::invoke-default-debugger), in process repl-thread(14).
> > Type :POP to abort, :R for a list of available restarts.
> > Type :? for other options.
> 1 > :q
> ; Evaluation aborted on #<type-error #x30200A694DAD>.
> cl-user>
>
> --
> __Pascal Bourguignon__ http://www.informat...
> "The factory of the future will have only two employees, a man and a
> dog. The man will be there to feed the dog. The dog will be there to
> keep the man from touching the equipment." -- Carl Bass CEO Autodesk

Pascal J. Bourguignon

9/27/2015 10:27:00 PM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> CL-USER 1 > (defun tr (n p)
> (unless (zerop p)
> (tr (1+ n) (1- p))
> (format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
> TR
>
> CL-USER 2 > (tr 0 4)
> *
> ***
> *****
> *******
> NIL
>
> CL-USER 3 > (tr 'hello 0)
> NIL
>
> CL-USER 4 > (tr 'hello 1)
>
> Error: In 1+ of (HELLO) arguments should be of type NUMBER.
> 1 (continue) Return a value to use.
> 2 Supply a new argument.
> 3 (abort) Return to level 0.
> 4 Return to top loop level 0.
>
> Type :b for backtrace or :c <option number> to proceed.
> Type :bug-form "<subject>" for a bug report template or :? for other options.

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

William James

9/28/2015 5:16:00 AM

0

Pascal J. Bourguignon wrote:

> cl-user> (defun tr (n p)
> (unless (zerop p)
> (tr (1+ n) (1- p))
> (format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
> tr
> cl-user> (tr 0 4)
> *
> ***
> *****
> *******

MatzLisp (Ruby):

def tr n, p
unless p.zero?
tr n.next, p.pred
puts "#{' ' * n}#{'*' * (p*2).pred}#{' ' * n}"
end
end

tr 0,4
*
***
*****
*******


Another way:

def tr n
(1..n).each{|i| puts ('*'*(i*2-1)).center n*2-1}
end

tr 4
*
***
*****
*******

--
[W]e had enough employees who made more than 85 to fill all the openings. The
highest score that any of the blacks scored on the test was 11. The lowest
score that any black made on the test was 4. All four of those blacks went
into skilled-trades training.
https://archive.org/download/TheOldmanArchives/oldman29-...

gengyangcai

9/28/2015 11:20:00 AM

0

Error while reading: A comma appears outside the scope of a backquote (or there are too many commas).

CL-USER 1 > def tr n, p

Error: A comma appears outside the scope of a backquote (or there are too many commas).
1 (continue) Ignore the comma.
2 (abort) Return to level 0.
3 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options. unless p.zero?
tr n.next, p.pred
puts "#{' ' * n}#{'*' * (p*2).pred}#{' ' * n}"
end
end

tr 0,4
*
***
*****
*******

CL-USER 1 : 1 > def tr n

Error: Undefined operator DEF in form (DEF TR N).
1 (continue) Try invoking DEF again.
2 Return some values from the form (DEF TR N).
3 Try invoking DSPEC:DEF with the same arguments.
4 Set the macro-function of DEF to the macro-function of DSPEC:DEF.
5 Try invoking something other than DEF with the same arguments.
6 Set the symbol-function of DEF to another function.
7 Set the macro-function of DEF to another function.
8 (abort) Return to level 1.
9 Return to debug level 1.
10 Ignore the comma.
11 Return to level 0.
12 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options. (1..n).each{|i| puts ('*'*(i*2-1)).center n*2-1}
end

tr 4
*
***
*****
*******


CL-USER 2 : 2 >



On Monday, September 28, 2015 at 1:18:21 PM UTC+8, WJ wrote:
> Pascal J. Bourguignon wrote:
>
> > cl-user> (defun tr (n p)
> > (unless (zerop p)
> > (tr (1+ n) (1- p))
> > (format t "~V@{ ~}~*~V@{*~}~%" n '() (1- (* 2 p)) '())))
> > tr
> > cl-user> (tr 0 4)
> > *
> > ***
> > *****
> > *******
>
> MatzLisp (Ruby):
>
> def tr n, p
> unless p.zero?
> tr n.next, p.pred
> puts "#{' ' * n}#{'*' * (p*2).pred}#{' ' * n}"
> end
> end
>
> tr 0,4
> *
> ***
> *****
> *******
>
>
> Another way:
>
> def tr n
> (1..n).each{|i| puts ('*'*(i*2-1)).center n*2-1}
> end
>
> tr 4
> *
> ***
> *****
> *******
>
> --
> [W]e had enough employees who made more than 85 to fill all the openings. The
> highest score that any of the blacks scored on the test was 11. The lowest
> score that any black made on the test was 4. All four of those blacks went
> into skilled-trades training.
> https://archive.org/download/TheOldmanArchives/oldman29-...