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!
need some help in lisp
Here is a pair of functions that might do the job:
And a sample run in the newLisp environment:
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 ))
I have not exhaustively tested the code, and I'm sure it can be improved.> (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