Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help!!

4 views
Skip to first unread message

Ulrich Nilsson

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to

Hi

Does anyone have the solution to this problem:

Write a funktion that answers this question
(binary-to-decimal '(1 1 0 1)) --> 13

Martti Halminen

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to

Could you be more specific? What is your problem?

a) you don't know the algorithm you are trying to implement?
b) you don't know how to implement a known algorithm in Lisp?
c) your attempt does something odd that you don't understand?
d) you are too lazy to do your own homework?

If this is case c), post your attempt so that we can see what is going
wrong!
In case a), the trick is to iterate through the list backwards, raising
2 to increasing powers whenever the element isn't 0, and summing the
results.
Cases b and d require more effort from the originator. Should be doable
in 5 lines of code, depending on your style.

Larry Hunter

unread,
Feb 18, 1998, 3:00:00 AM2/18/98
to

Ulrich Nilsson asks:

Does anyone have the solution to this problem:
Write a funktion that answers this question
(binary-to-decimal '(1 1 0 1)) --> 13

How about

(defun binary-to-decimal (bits)
(read-from-string (format nil "#b~{~d~}" bits)))

Hint: if you want turn the above in as homework, be prepared to explain what
it does.

--
Lawrence Hunter, PhD.
National Library of Medicine phone: +1 (301) 496-9303
Bldg. 38A, 9th fl, MS-54 fax: +1 (301) 496-0673
Bethesda. MD 20894 USA email: hun...@nlm.nih.gov

Michael Pazzani

unread,
Feb 21, 1998, 3:00:00 AM2/21/98
to

In article <34eac7a9.0@alius>, Ulrich Nilsson <na9...@student.hgs.se> wrote:
>Hi

>
>Does anyone have the solution to this problem:
>
>Write a funktion that answers this question
>(binary-to-decimal '(1 1 0 1)) --> 13
>
>

I answered one of these once. A week later I got this e-mail. Since
then I let students do their own homework and use this example in my
classes as a bad example of how to do homework.


Subject: easy money for you

I'm looking for a lisp programmer to help me complete my project.
If you decide to accept, a mail-order-check will by made out to you and
mailed to your home
address. You can reply with your home address if you decide to complete
this project. I am willing to pay $60.00 for fully functional codes.
Must have all of the code by september 28, 1997.


1. (use recursion for these, no iterative function)
a). Define a function nth-element that returns the nth element from
an arbitrary list. For example

(nth-element 2' (lisp (is really) ((fun))

should return (is really). assume correct expression, but
(nth-element 0 anylist) should return nil, also if number is larger than
length list, shoul return nil.

b). Define a function every-other-number that take a list and return a
list with every other number from the original list. for example:

(every-other-number '(this 5 is really 2 insane 3 41 28))
should return this list (2 41). If there is less than two number
in the list, return nil. You can write more than one function (another
function) to call from
every-other-number that does part of the work for you.


Neil Schemenauer

unread,
Feb 21, 1998, 3:00:00 AM2/21/98
to

On 21 Feb 1998 09:40:08 -0800, Michael Pazzani <paz...@ultra-pan.ics.uci.edu> wrote:

>Subject: easy money for you
>
> I'm looking for a lisp programmer to help me complete my project.
>If you decide to accept, a mail-order-check will by made out to you and
>mailed to your home
>address. You can reply with your home address if you decide to complete
>this project. I am willing to pay $60.00 for fully functional codes.
>Must have all of the code by september 28, 1997.

(define (nth-element n l)
(cond
((null? l) '())
((< n 1) '())
((eq? n 1) (car l))
(else (nth-element (- n 1) (cdr l)))))

(define (every-other-number l)
(define (filter p l)
(cond
((null? l) '())
((p (car l)) (cons (car l) (filter p (cdr l))))
(else (filter p (cdr l)))))

(define (odd-elements l)
(cond
((null? l) '())
((null? (cdr l)) l)
(else (cons (car l) (odd-elements (cddr l))))))

(define (even-elements l)
(cond
((null? l) '())
(else (odd-elements (cdr l)))))

(even-elements (filter number? l)))


That would have been easy money. Too bad I'm too late. :) Why
the hell do people pay money to go to school and refuse to learn
anything?


Neil

Erik Naggum

unread,
Feb 22, 1998, 3:00:00 AM2/22/98
to

* Neil Schemenauer

| That would have been easy money. Too bad I'm too late. :) Why the hell
| do people pay money to go to school and refuse to learn anything?

so that they can bring their diplomas and get jobs where people who also
refuse to learn anything (their managers) pay _them_ lots of money, for a
change. hey, it keeps a lot of the population happy. can't be wrong.

#:Erik
--
God grant me serenity to accept the code I cannot change,
courage to change the code I can, and wisdom to know the difference.

David D. Smith

unread,
Mar 4, 1998, 3:00:00 AM3/4/98
to

In article <34eac7a9.0@alius>, "Ulrich Nilsson" <na9...@student.hgs.se> wrote:

> Hi
>
> Does anyone have the solution to this problem:
>
> Write a funktion that answers this question
> (binary-to-decimal '(1 1 0 1)) --> 13

(defun binary-to-decimal (bits)
(reduce #'(lambda (a b) (logior (ash a 1) b)) bits :initial-value 0))

? (binary-to-decimal '(1 1 0 1))
13
?

0 new messages