[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: small prog big probs

William James

5/14/2015 5:07:00 AM

Kent M. Pitman wrote:

> (defun write-list (elements)
> (loop for comma? first nil then t
> for element in elements
> when comma?
> do (write-string ", ")
> do (write element)))


Gauche Scheme:

Using recursion:

(define (write-list items)
(let go ((sep ""))
(when (pair? items)
(display sep)
(display (pop! items))
(go ", "))))

gosh> (write-list (iota 8))
0, 1, 2, 3, 4, 5, 6, 7#<undef>


Using while:

(define (write-list items)
(while (pair? items)
(display (pop! items))
(when (pair? items) (display ", "))))


Using pair-for-each:

(use srfi-1 :only (pair-for-each))

(define (write-list items)
(pair-for-each
(^x (display (car x)) (when (pair? (cdr x)) (display ", ")))
items))

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html
2 Answers

William James

11/27/2015 6:37:00 PM

0

WJ wrote:

> Kent M. Pitman wrote:
>
> > (defun write-list (elements)
> > (loop for comma? first nil then t
> > for element in elements
> > when comma?
> > do (write-string ", ")
> > do (write element)))
>
>
> Gauche Scheme:
>
> Using recursion:
>
> (define (write-list items)
> (let go ((sep ""))
> (when (pair? items)
> (display sep)
> (display (pop! items))
> (go ", "))))
>
> gosh> (write-list (iota 8))
> 0, 1, 2, 3, 4, 5, 6, 7#<undef>
>
>
> Using while:
>
> (define (write-list items)
> (while (pair? items)
> (display (pop! items))
> (when (pair? items) (display ", "))))
>
>
> Using pair-for-each:
>
> (use srfi-1 :only (pair-for-each))
>
> (define (write-list items)
> (pair-for-each
> (^x (display (car x)) (when (pair? (cdr x)) (display ", ")))
> items))

Ocaml:

let rec write_list print = function
[] -> ()
| x::(y::_ as rest) -> print x; print_string ", "; write_list print rest
| [x] -> print x ;;

write_list print_int [2;3;4];;
===>
2, 3, 4

--
[Jesse Jackson] would spit into the food of white patrons he hated and then
smilingly serve it to them. He did this, he said, "because it gave me
psychological gratification." -- Life Magazine, 1969-11-29

William James

11/27/2015 7:45:00 PM

0

WJ wrote:

> WJ wrote:
>
> > Kent M. Pitman wrote:
> >
> > > (defun write-list (elements)
> > > (loop for comma? first nil then t
> > > for element in elements
> > > when comma?
> > > do (write-string ", ")
> > > do (write element)))
> >
> >
> > Gauche Scheme:
> >
> > Using recursion:
> >
> > (define (write-list items)
> > (let go ((sep ""))
> > (when (pair? items)
> > (display sep)
> > (display (pop! items))
> > (go ", "))))
> >
> > gosh> (write-list (iota 8))
> > 0, 1, 2, 3, 4, 5, 6, 7#<undef>
> >
> >
> > Using while:
> >
> > (define (write-list items)
> > (while (pair? items)
> > (display (pop! items))
> > (when (pair? items) (display ", "))))
> >
> >
> > Using pair-for-each:
> >
> > (use srfi-1 :only (pair-for-each))
> >
> > (define (write-list items)
> > (pair-for-each
> > (^x (display (car x)) (when (pair? (cdr x)) (display ", ")))
> > items))
>
> Ocaml:
>
> let rec write_list print = function
> [] -> ()
> | x::(y::_ as rest) -> print x; print_string ", "; write_list print rest
> | [x] -> print x ;;
>
> write_list print_int [2;3;4];;
> ===>
> 2, 3, 4

Shorter:

let rec write_list print = function
[] -> ()
| [x] -> print x
| x::rest -> print x; print_string ", "; write_list print rest ;;