[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: define anywhere
From: |
Damien Mattei |
Subject: |
Re: define anywhere |
Date: |
Sat, 12 Jun 2021 01:43:29 +0200 |
hello Linus,
finally i tested your code with my program and it works.
define worked in if in ssigma-sol-approx
( in
*Preview:*
;; (load "SssRec.scm")
;;(use-modules (syntax define));;(use-modules
(guile/define))(use-modules (guile define))
(include "first-and-rest.scm")(include "list.scm")(include
"postfix.scm")(include "let.scm")(include "definition.scm")(include
"guile/array.scm")(include "block.scm")
;; if data are disordered the algo works also
;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723
889 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027));;
scheme@(guile-user)> (start-ssigma-sol-approx-linus L-init 19836);;
$1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285
5027)(define (start-ssigma-sol-approx-linus L t) ;; Sub Set Sum
problem (find solution or approximation)
;; { } are for infix notation as defined in SRFI 105
;; <+ and := are equivalent to (define var value)
{ best-sol <+ (lambda (L1 L2)
{s1 <+ (apply + L1)}
{s2 <+ (apply + L2)}
(if {(abs {t - s1}) <= (abs {t - s2})}
L1
L2)) }
;; := is the same macro as <+
{ best-sol3 := (lambda (L1 L2 L3)
{L22 <+ (best-sol L2 L3)}
(best-sol L1 L22)) }
{ ssigma-sol-approx <+ (lambda (L)
;; def is a macro for declared but unasigned
variable, it is
same as (define var '())
;;(def c)
;;(def R)
(if (null? L)
L
($ (define c (first L)) ;; $ = begin
(if {c = t}
(list c) ;; c is the solution
($ (define R (rest L))
(if {c > t}
(best-sol (list c)
(ssigma-sol-approx R)) ;; c is to big to
be a solution but could be an approximation
;; c < t at this point, 3
possibilities :
;; c is the best solution
;; c is part of the solution or
his approximation
;; or c is not part of solution or
his approximation
(best-sol3 (list c) ;; c is the
best solution
;; c part of solution
or is approximation
(cons c
(start-ssigma-sol-approx-linus R {t - c})) ;; we have
to find a solution or an approximation for t-c now
;; c is not part of
solution or his approximation
(ssigma-sol-approx
R)))))))) }
;; start the function
(ssigma-sol-approx L))
without you module, my solution was:
https://github.com/damien-mattei/library-FunctProg/blob/master/SssRec.scm
*Preview:*
;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723
889 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027));;
scheme@(guile-user)> (start-ssigma-sol-approx-basic L-init 19836);;
$1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285
5027);; scheme@(guile-user)> (apply + $1);; $2 = 19836(define
(start-ssigma-sol-approx-basic L t) ;; Sub Set Sum problem (find
solution or approximation)
;; { } are for infix notation as defined in SRFI 105
;; <+ and := are equivalent to (define var value)
{ best-sol <+ (lambda (L1 L2)
{s1 <+ (apply + L1)}
{s2 <+ (apply + L2)}
(if {(abs {t - s1}) <= (abs {t - s2})}
L1
L2)) }
;; := is the same macro as <+
{ best-sol3 := (lambda (L1 L2 L3)
{L22 <+ (best-sol L2 L3)}
(best-sol L1 L22)) }
{ ssigma-sol-approx <+ (lambda (L)
;; def is a macro for declared but unasigned
variable, it is
same as (define var '())
(def c)
(def R)
(if (null? L)
L
($ {c <- (first L)} ;; $ = begin
(if {c = t}
(list c) ;; c is the solution
($ {R <- (rest L)}
(if {c > t}
(best-sol (list c)
(ssigma-sol-approx R)) ;; c is to big to
be a solution but could be an approximation
;; c < t at this point, 3
possibilities :
;; c is the best solution
;; c is part of the solution or
his approximation
;; or c is not part of solution or
his approximation
(best-sol3 (list c) ;; c is the
best solution
;; c part of solution
or is approximation
(cons c
(start-ssigma-sol-approx-basic R {t - c})) ;; we have
to find a solution or an approximation for t-c now
;; c is not part of
solution or his approximation
(ssigma-sol-approx
R)))))))) }
;; start the function
(ssigma-sol-approx L))
just had a few problem making Guile 3.0 find the module from an other sub
directory, but not the falt of you module, my config i think?
Damien
On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:
> I implemented this hack before guile 3 got defines in function bodies:
> https://hg.sr.ht/~bjoli/guile-define
>
> Even I guile 3 it allows a more liberal placement of define, but it won't
> work for things like bodies of imported macros (like match)
> --
> Linus Björnstam
>
> On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > hello,
> > i'm was considering that i want to be able to define a variable
> > anywhere in
> > code, the way Python do. Few scheme can do that (Bigloo i know)
> > ( the list here is not exact:
> >
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > )
> > is it possible in guile to do it? i do not know, so could it be added
> > to
> > the language specification for future release?
> >
> > regards,
> > Damien
> >
>
- Re: define anywhere, (continued)