need some help in lisp

For the Compleat Fan
Locked
bigx
Posts: 2
Joined: Mon Oct 17, 2005 3:58 pm

need some help in lisp

Post by bigx »

hello
i'm a french newbie in lisp
i need some help to realise a function wich gives me the exact (or the nearest) result by using a list of numbers given in argument

something like this
(defun good-count (n l)
...
))))))))))))))))))

where n is the number i want to found and l is the list of numbers i have to use to found n
i can also use results of operation on the numbers of the list l
i can only use +,-,/,* operations

thanks for your help
and apologize me for my bad english 'im french

see you!

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Here is a pair of functions that might do the job:

Code: Select all

(define (good-count N L)
  (let
    ;if N=3.3 and L=(1 3 4) then pairs=((1 2.3) (3 0.3) (4 0.7))
    ( pairs (map (fn (x) (list x (abs (sub x N)))) L) )
  ;body of let
    ((min-pair pairs) 0) ))

(define (min-pair L)
  (let
    ( MIN-PAIR (L 0) )
  ;body of let
    (dolist (PAIR (rest L))
      (if (< (PAIR 1) (MIN-PAIR 1)) (set 'MIN-PAIR PAIR)) )
    MIN-PAIR ))
And a sample run in the newLisp environment:
> (set 'L '(1 2 3 4 5 6))
(1 2 3 4 5 6)
> (good-count 2.3 L)
2
> (good-count 2.5 L)
2
> (good-count 2.6 L)
3
> (good-count -1 L)
1
> (good-count 100 L)
6
> (good-count -100 L)
1
> (good-count 0 L)
1
> (good-count .5 L)
1
> (good-count 1.5 L)
1
> (good-count 1.51 L)
2
I have not exhaustively tested the code, and I'm sure it can be improved.

bigx
Posts: 2
Joined: Mon Oct 17, 2005 3:58 pm

Post by bigx »

thank it will be useful, but i think i didn't explain it very well
an example

if i have the list L : '(1 100 75 2 9 10 9)
and the number N as 256

i have to find the sequence of operations as below(or something wich gives me the same result):
(2*100)+75-(10+9)=256

thanks for your help!

statik
Posts: 58
Joined: Thu Apr 14, 2005 1:12 am

Post by statik »

If I am not mistaken, you want to search that list of numbers, collecting a group, and forming them into various different equations until you've discovered a valid formula that evaluates to your chosen number. Am I right in my assumptions?
-statik

Locked