• (3-digit combination) Lock.lsp (in Gauche Scheme)

    From HenHanna@HenHanna@gmail.com to comp.lang.lisp,comp.lang.scheme on Mon Feb 26 03:47:28 2024
    From Newsgroup: comp.lang.scheme

    (pls suggest improvements. Thanks!)


    PS C:\Lisp\LockPuz> gosh -I .
    gosh> (load "Lock.lsp")
    1000 ( Now applying Constraint: ) ((6 8 2) (1 1))
    192 ( Now applying Constraint: ) ((6 1 4) (1 0))
    38 ( Now applying Constraint: ) ((2 0 6) (2 0))
    1
    ((0 4 2))


    (define (Score X Y)
    (list (apply + (map (lambda (y) (if (member y X) 1 0)) Y))
    (count zero? (map - X Y))))

    (define (MapCan f Lis) (apply append (map f Lis)))

    (define (run)
    (let* ((Const '(((6 8 2) (1 1))
    ((6 1 4) (1 0))
    ((2 0 6) (2 0)) ))
    (dig (iota 10))
    (Cand (MapCan (lambda (x)
    (map (lambda (i) (cons x i))
    (MapCan (lambda (y) (map (lambda (z) (list y z))
    dig)) dig))) dig)))
    (dolist (req Const)
    (format #t "~T ~S ~T ( Now applying Constraint: ) ~T ~S ~%"
    (length Cand) req)
    (set! Cand
    (filter (lambda (c) (equal? (Score c (car req)) (cadr req)))
    Cand)))
    (format #t "~T ~S ~% ~T ~S ~%" (length Cand) Cand)))
    (run)
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From HenHanna@HenHanna@gmail.com to comp.lang.lisp,comp.lang.scheme on Mon Feb 26 04:23:14 2024
    From Newsgroup: comp.lang.scheme

    On 2/26/2024 3:47 AM, HenHanna wrote:
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a (pls suggest improvements.-a Thanks!)


    PS C:\Lisp\LockPuz>-a-a-a-a-a-a-a gosh-a-a-a -I-a-a .
    gosh> (load "Lock.lsp")
    -a-a-a-a-a-a-a-a 1000-a-a-a ( Now applying Constraint: )-a-a-a-a-a-a-a ((6 8 2) (1 1))
    -a-a-a-a-a-a-a-a 192-a-a-a-a ( Now applying Constraint: )-a-a-a-a-a-a-a ((6 1 4) (1 0))
    -a-a-a-a-a-a-a-a 38-a-a-a-a-a ( Now applying Constraint: )-a-a-a-a-a-a-a ((2 0 6) (2 0))
    -a-a-a-a-a-a-a-a 1
    -a-a-a-a-a-a-a-a ((0 4 2))


    (define (Score X Y)
    -a (list (apply + (map (lambda (y) (if (member y X) 1 0))-a-a-a Y))
    -a-a-a-a-a-a-a (count zero?-a (map - X Y))))

    (define (MapCan f Lis)-a-a-a (apply append (map f Lis)))

    (define (run)
    -a (let* ((Const '(((6 8 2)-a (1 1))
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a ((6 1 4)-a (1 0))
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a ((2 0 6)-a (2 0)) ))
    -a-a-a-a-a-a-a-a (dig (iota 10))

    -a-a-a-a-a-a-a-a (Cand (MapCan (lambda (x)
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a (map (lambda (i) (cons x i))
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a (MapCan (lambda (y) (map (lambda (z) (list y z))
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a-a dig)) dig))) dig)))


    more readable as:

    (define (conv3 x)
    (list (floor/ x 100) (mod (floor/ x 10) 10) (mod x 10)))

    (define Thousand (map conv3 (iota 1000)))

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From HenHanna@HenHanna@dev.null to comp.lang.lisp,comp.lang.scheme on Fri Mar 1 02:11:33 2024
    From Newsgroup: comp.lang.scheme

    i enjoyed working on the problem of coming up with a 1-line Python code.
    We dont really have LINES in Lisp-Scheme... What would be a similar challenge in Lisp-Scheme?



    (define (Score X Y)
    (list (count list? (map (lambda (y) (member y X)) Y))
    (count zero? (map - X Y))))
    ; <--- Is there a better way? (using equal? instead of - ) ?
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.lisp,comp.lang.scheme on Thu Feb 29 18:28:44 2024
    From Newsgroup: comp.lang.scheme

    HenHanna <HenHanna@dev.null> writes:

    (define (Score X Y)
    (list (count list? (map (lambda (y) (member y X)) Y))
    (count zero? (map - X Y))))
    ; <--- Is there a better way? (using equal? instead of - ) ?

    This was my version:

    (define (score2 candidate answer)
    (let* ((well-placed (length (filter identity (map = candidate answer))))
    (wrongly-placed (- (length (lset-intersection = candidate answer))
    well-placed)))
    (values well-placed wrongly-placed)))

    Maybe there is something better.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From HenHanna@HenHanna@dev.null to comp.lang.lisp,comp.lang.scheme on Fri Mar 1 02:56:20 2024
    From Newsgroup: comp.lang.scheme

    (define (Score X Y)
    (list (count (lambda (y) (member y X)) Y)
    (count equal? X Y)))



    Where can i find doc for COUNT ?

    The section in the Gauche manual is so short!

    but now i see that it explains what i was looking for
    > the count of times pred returned true is returned.


    https://practical-scheme.net/gauche/man/gauche-refe/Pairs-and-lists.html


    Function: count pred clist1 clist2 rCa

    [R7RS list] A procedure pred is applied to the n-th element of given lists, from n is zero to the length of the the shortest finite list in the given lists, and the count of times pred returned true is returned.

    (count even? '(3 1 4 1 5 9 2 5 6)) rcA 3
    (count < '(1 2 4 8) '(2 4 6 8 10 12 14 16)) rcA 3

    At least one of the argument lists must be finite:

    (count < '(3 1 4 1) (circular-list 1 10)) rcA 2
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.lisp,comp.lang.scheme on Thu Feb 29 19:39:06 2024
    From Newsgroup: comp.lang.scheme

    FYI, here is an interesting paper about playing Mastermind using a SAT
    solver, beating other approaches. Mastermind turns out to be well-known
    to be NP-complete (a quick web search found that).

    https://www.seas.upenn.edu/~ncollina/Mastermind.pdf
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From HenHanna@HenHanna@dev.null to comp.lang.lisp,comp.lang.scheme on Sat Mar 2 09:40:04 2024
    From Newsgroup: comp.lang.scheme

    Paul Rubin wrote:

    FYI, here is an interesting paper about playing Mastermind using a SAT solver, beating other approaches. Mastermind turns out to be well-known
    to be NP-complete (a quick web search found that). https://www.seas.upenn.edu/~ncollina/Mastermind.pdf


    thank you... i'll take a look.



    What are some relevant sections in Knuth's book [Satisfiability] ?

    Sudoku is mentioned several times, but not Mastermind.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.lisp,comp.lang.scheme on Sat Mar 2 10:44:28 2024
    From Newsgroup: comp.lang.scheme

    HenHanna <HenHanna@dev.null> writes:
    What are some relevant sections in Knuth's book [Satisfiability] ?

    That book discusses the algorithms used in SAT solvers, but from what
    little I know, they are all tweaks and improvements on the DPLL
    algorithm from the 1960s:

    https://en.wikipedia.org/wiki/DPLL_algorithm

    Thus, if you want to study the workings of SAT solvers, you could start
    by looking at MiniSAT which is one of the simplest. If you want to
    learn how to use them, this is good:

    https://yurichev.com/writings/SAT_SMT_by_example.pdf
    --- Synchronet 3.22a-Linux NewsLink 1.2