[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Syntax Question

William James

7/26/2015 11:09:00 AM

Edi Weitz wrote:

> > In the code below, the call (in caps) to helper gives me the error
> > "Variable HELPER has no value". Am working on transitioning from
> > scheme to lisp. Could someone explain why it believes helper has no
> > value? Thank you!
> >
> > (defun adj-list-to-edge-list (edgelst)
> > (let (
> > (helper (lambda (edges node)
> > (cond ((null edges) nil)
> > (t (cons (list node (first edges))
> > (HELPER (rest edges)
> > node))))))
> > )
> > (cond ((null edgelst) nil)
> > (t (append (funcall helper
> > (rest (first edgelst))
> > (first (first edgelst)))
> > (adj-list-to-edge-list (rest
> > edgelst)))))))
> >
> > Examples of how code works:
> > (defparameter adjlist '((a b c) (b e) (c d) (d b c e)
> > (e d f h) (f e) (g h) (h e g)))
> >
> > (adj-list-to-edge-list '((a b))) => ((A B))
> > (adj-list-to-edge-list '((a b c d))) => ((A B) (A C) (A D))
> > (adj-list-to-edge-list adjlist) => ((A B) (A C) (B E) (C D) (D B) (D C)
> > (D E) (E D) (E F) (E H) (F
> > E) (G H)
> > (H E) (H G))
>
> It would be somewhat shorter do write it like this
>
> (defun foo (list)
> (loop for (first . rest) in list
> nconc (loop for x in rest
> collect (list first x))))
>
> if I understand you correctly.
>
> Don't try to translate from Scheme to CL verbatim, try to learn the
> language constructs CL has to offer.

Gauche Scheme:

(define (foo lists)
(append-map
(lambda (xs) (map (cut list (car xs) <>) (cdr xs)))
lists))

(foo '((a b c d) (x y z) (q p)))
===>
((a b) (a c) (a d) (x y) (x z) (q p))


Another way:

(use srfi-42 :only (list-ec))

(define (foo lists)
(list-ec
(: xs lists)
(: y (cdr xs))
(list (car xs) y)))

--
For two years ... he was held in solitary confinement in the Toronto West
Detention Centre, on the pretext that he is a threat to national security....
[A] court in Mannheim sentenced him to five years imprisonment for the crime of
"popular incitement" under Germany's notorious "Holocaust denial" statute.
http://www.revisionists.com/revisionists/z...
1 Answer

William James

5/24/2016 11:39:00 PM

0

WJ wrote:

> Edi Weitz wrote:
>
> > > In the code below, the call (in caps) to helper gives me the error
> > > "Variable HELPER has no value". Am working on transitioning from
> > > scheme to lisp. Could someone explain why it believes helper has no
> > > value? Thank you!
> > >
> > > (defun adj-list-to-edge-list (edgelst)
> > > (let (
> > > (helper (lambda (edges node)
> > > (cond ((null edges) nil)
> > > (t (cons (list node (first edges))
> > > (HELPER (rest edges)
> > > node))))))
> > > )
> > > (cond ((null edgelst) nil)
> > > (t (append (funcall helper
> > > (rest (first edgelst))
> > > (first (first edgelst)))
> > > (adj-list-to-edge-list (rest
> > > edgelst)))))))
> > >
> > > Examples of how code works:
> > > (defparameter adjlist '((a b c) (b e) (c d) (d b c e)
> > > (e d f h) (f e) (g h) (h e g)))
> > >
> > > (adj-list-to-edge-list '((a b))) => ((A B))
> > > (adj-list-to-edge-list '((a b c d))) => ((A B) (A C) (A D))
> > > (adj-list-to-edge-list adjlist) => ((A B) (A C) (B E) (C D) (D B) (D C)
> > > (D E) (E D) (E F) (E H) (F
> > > E) (G H)
> > > (H E) (H G))
> >
> > It would be somewhat shorter do write it like this
> >
> > (defun foo (list)
> > (loop for (first . rest) in list
> > nconc (loop for x in rest
> > collect (list first x))))
> >
> > if I understand you correctly.
> >
> > Don't try to translate from Scheme to CL verbatim, try to learn the
> > language constructs CL has to offer.
>
> Gauche Scheme:
>
> (define (foo lists)
> (append-map
> (lambda (xs) (map (cut list (car xs) <>) (cdr xs)))
> lists))
>
> (foo '((a b c d) (x y z) (q p)))
> ===>
> ((a b) (a c) (a d) (x y) (x z) (q p))
>
>
> Another way:
>
> (use srfi-42 :only (list-ec))
>
> (define (foo lists)
> (list-ec
> (: xs lists)
> (: y (cdr xs))
> (list (car xs) y)))

OCaml:

let foo lists =
lists
|> List.map (fun (x::xs) -> List.map (fun y -> x,y) xs)
|> List.concat ;;

foo [["a";"b";"c";"d"];["x";"y";"z"];["q";"p"]] ;;

===>
[("a", "b"); ("a", "c"); ("a", "d"); ("x", "y"); ("x", "z"); ("q", "p")]

--
Again, commenters focused on the double standard whereby Jews advocate
multiculturalism and immigration for Europe, but have Israel as an avowedly
Jewish state to fall back on...
theoccidentalobserver.net/2015/11/douglas-murrays-warning-to-the-jewish-community