From: hWnd
Subject: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144315822.566007.266100@i39g2000cwa.googlegroups.com>
Hi all.

I got really interested in Lisp and I bought HtDP from Amazon some weks
ago.

I used to solve all excersises after the chapters, but now I can't
figure out how to cope with some. Especially, I don't get how to cons
lists when there is some condition specifying the content of the list.
Ok, bad, explanation -- consider the following:

---
The cash registers at many grocery stores talk to customers. The
register's computer receives the number of cents that the customer must
pay and then builds a list with the following five items:

   1. the dollar amount;
   2. the symbol 'dollar if the dollar amount is 1 and 'dollars
otherwise;
   3. the symbol 'and;
   4. the cent amount; and
   5. the symbol 'cent if the cent amount is 1 and 'cents otherwise.

Develop the function controller, which consumes a number and produces a
list according to the above description. For example, if the amount is
$1.03, then the cash register evaluates (controller 103):

(controller 103)
;; expected value:
(cons 1 (cons 'dollar (cons 'and (cons 3 (cons 'cents empty)))))
---
(excersise 10.1.9 from
http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-14.html#node_sec_10.1)

From: ·········@gmail.com
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144320613.895277.106630@v46g2000cwv.googlegroups.com>
>
> (controller 103)
> ;; expected value:
> (cons 1 (cons 'dollar (cons 'and (cons 3 (cons 'cents empty)))))
> ---
It's easy when you can calculate the num of dollars and number of cents
use floor func:

(defun controler(n)
 (multiple-value-bind  (dollar   cent) (floor  n 100)
 	(cons dollar  (cons (if (eql dollar 1) 'dollar 'dollars)
                          (cons 'and  (cons cent (cons (if (eql cent 1)
'cent 'cents) empty)))))))
From: Thomas Womack
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <eGe*GTtdr@news.chiark.greenend.org.uk>
In article <························@i39g2000cwa.googlegroups.com>,
hWnd <······@smilyanov.net> wrote:

>Develop the function controller, which consumes a number and produces a
>list according to the above description. For example, if the amount is
>$1.03, then the cash register evaluates (controller 103):
>
>(controller 103)
>;; expected value:
>(cons 1 (cons 'dollar (cons 'and (cons 3 (cons 'cents empty)))))

My only guess as to what you might be doing wrong is putting
unnecessary brackets around the arguments to the (if cond then else)
statement.

(setq empty nil); might not be needed in non-emacs lisp

(defun controller (x)
  (let ((dollars (/ x 100))
	(cents (% x 100)))	
    (cons dollars 
	  (cons (if (= dollars 1) 'dollar 'dollars) 
		(cons 'and 
		      (cons cents 
			    (cons (if (= cents 1) 'cent 'cents) empty)))))))

works for me.

Tom
From: Jens Axel Søgaard
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <4434f45d$0$38669$edfadb0f@dread12.news.tele.dk>
Thomas Womack wrote:
> hWnd <······@smilyanov.net> wrote:

> My only guess as to what you might be doing wrong is putting
> unnecessary brackets around the arguments to the (if cond then else)
> statement.
> 
> (setq empty nil); might not be needed in non-emacs lisp
> 
> (defun controller (x)
>   (let ((dollars (/ x 100))
> 	(cents (% x 100)))	
>     (cons dollars 
> 	  (cons (if (= dollars 1) 'dollar 'dollars) 
> 		(cons 'and 
> 		      (cons cents 
> 			    (cons (if (= cents 1) 'cent 'cents) empty)))))))
> 
> works for me.

It doesn't work in the context of the book though. Section 10
is about list processing functions and gives a template to use
for defining such functions. The exercises gives the user a
chance to use the template.

-- 
Jens Axel S�gaard
From: hWnd
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144322758.344216.195240@u72g2000cwu.googlegroups.com>
The contract, purpose, and header should be something like that:
;;controller: number->a-list
;;consumes a sum of cents and produces a list according to the [above]
description
;;(define (controller cents)
I cant' formulate the template.
From: Jens Axel Søgaard
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <4434ffd0$0$38726$edfadb0f@dread12.news.tele.dk>
hWnd wrote:
> The contract, purpose, and header should be something like that:
> ;;controller: number -> a-list
> ;;  consumes a sum of cents and produces a list according to the [above]
>     description
> ;;(define (controller cents)
> I cant' formulate the template.

It is true that the output is a list. To be be precise it
is a five-element-list. To be even more precise, it is a description:

    Datadefinition:

    A DESCRIPTION is a

      (cons dollar-amount
         (cons dollar/dollars
               (cons and-symbol
                     (cons cent-amount
                           (cons cent/cents
                                 empty)))))

    where

      dollar-amount is a number.
      dollar-symbol is either 'dollar or 'dollars
      and-symbol is the symbol 'and
      cent-amount is a number
      cent/cents is either 'cent or 'cents

Your template thus begins

; controller: number -> a-list
;   consumes a sum of cents and produces a list according
;   to the [above] description

(define (controller cents)
    (cons dollar-amount
       (cons dollar/dollars
             (cons 'and
                   (cons cent-amount
                         (cons cent/cents
                               empty))))))

The next question is how to calculate what dollar-amount,
dollar/dollars, cent-amount and cent/cents are given the
input cents.

If cents is 103 how can one calculate the dollar amount 1?
If cents is 103 how can one calculate the cent amount 3?

-- 
Jens Axel S�gaard
From: hWnd
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144326207.991187.19080@t31g2000cwb.googlegroups.com>
(quotient 103 100) ;returns 1
(remainder 103 100) ;returns 3
From: Jens Axel Søgaard
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <44350a2c$0$38709$edfadb0f@dread12.news.tele.dk>
hWnd wrote:
> (quotient 103 100) ;returns 1
> (remainder 103 100) ;returns 3

Great. All you need now is to replace dollar/dollars with
a cond-expression.

-- 
Jens Axel S�gaard
From: hWnd
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144327292.900983.104860@t31g2000cwb.googlegroups.com>
Wow, it worked. But to be honest -- I'm a little bit disappointed. Is
this a good programming practice:

(cons
 (cond
   [(> 1 2) 1]
   [else 2])
 empty)

Actually that was the first thing I thought about, but I threw it off
quickly as a very ugly solution. So, actually it turns out that it is
ok?
From: Jens Axel Søgaard
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <44350fbc$0$38712$edfadb0f@dread12.news.tele.dk>
hWnd wrote:
> Wow, it worked. But to be honest -- I'm a little bit disappointed. Is
> this a good programming practice:
> 
> (cons
>  (cond
>    [(> 1 2) 1]
>    [else 2])
>  empty)

What is the cons for? And where are the 'dollar and 'dollars ?

-- 
Jens Axel S�gaard
From: hWnd
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <1144504721.392137.155240@j33g2000cwa.googlegroups.com>
@ Jens: sorry, I confused myself (w/ the latest post). Anyway, thanks
-- I got it.

Now I've another problem:

---
Lists may contain lists that contain lists and so on. Here is a data
definition that takes this idea to an extreme:

A deep-list is either
   1. s where s is some symbol or
   2. (cons dl empty), where dl is a deep list.

Develop the function depth, which consumes a deep list and determines
how many times cons was used to construct it.
---
(excersise 11.2.4 from
http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-15.html#node_sec_11.4)

Initially I thought that a "deep list" is something like: (cons 'foo
(cons 'bar (cons 'foobar empty))), so I created depth as:

(define (depth a-list)
  (cond
    [(empty? a-list) 0]
    [else (+ 1 (depth (cdr a-list)))]))

Later on, I found out that (probably) this is not the actual "deep
list". Now I think that it should be:

(cons (cons
       (cons
        (cons 'foo empty)
        empty)
       empty)
      empty)

But I can't invent the depth function. Would you give me some starting
point(s)?

I testes everything I could invent (e.g:
(define (depth a-list)
  (cond
    [(symbol? a-list) 0]
    [else (+ 1
             (depth (cdr a-list)))]))
), but with no success.
From: Pascal Bourguignon
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <874q1397hv.fsf@thalassa.informatimago.com>
"hWnd" <······@smilyanov.net> writes:

> @ Jens: sorry, I confused myself (w/ the latest post). Anyway, thanks
> -- I got it.
>
> Now I've another problem:
>
> ---
> Lists may contain lists that contain lists and so on. Here is a data
> definition that takes this idea to an extreme:
>
> A deep-list is either
>    1. s where s is some symbol or
>    2. (cons dl empty), where dl is a deep list.
>
> Develop the function depth, which consumes a deep list and determines
> how many times cons was used to construct it.
> ---
> (excersise 11.2.4 from
> http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-15.html#node_sec_11.4)
> [...]
> But I can't invent the depth function. Would you give me some starting
> point(s)?
>
> I testes everything I could invent (e.g:
> (define (depth a-list)
>   (cond
>     [(symbol? a-list) 0]
>     [else (+ 1
>              (depth (cdr a-list)))]))
> ), but with no success.


Rememer the fundamental equations of car, cdr, and cons:

  (eql (car (cons a b)) a)             ; [1]
  (eql (cdr (cons a b)) b)             ; [2]
  (equal (cons (car x) (cdr x)) x)     ; [3]


Most algorithms actually follow strictly the data structure.  
That is: Most algorithms actually FOLLOW STRICTLY the data structure.  

> A deep-list is either
>    1. s where s is some symbol or
>    2. (cons dl empty), where dl is a deep list.

  (define (depth dl)
      ;;  A deep-list is either
      (cond
        ;; 1. s where s is some symbol or
        ((symbol? dl)   0)
        ;; 2. (cons dl empty), where dl is a deep list.
        (else (+ 1 (depth (car dl)))))) ; according to [1]

When you put some data in the CAR of a CONS cell, you don't go
searching it in the CDR!
        

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"I have challenged the entire quality assurance team to a Bat-Leth
contest.  They will not concern us again."
From: Jens Axel Søgaard
Subject: Re: How to Design Programs: Problems with some of the excersises
Date: 
Message-ID: <4434f0dc$0$38664$edfadb0f@dread12.news.tele.dk>
hWnd wrote:

> The cash registers at many grocery stores talk to customers. The
> register's computer receives the number of cents that the customer must
> pay and then builds a list with the following five items:
> 
>    1. the dollar amount;
>    2. the symbol 'dollar if the dollar amount is 1 and 'dollars
> otherwise;
>    3. the symbol 'and;
>    4. the cent amount; and
>    5. the symbol 'cent if the cent amount is 1 and 'cents otherwise.
> 
> Develop the function controller, which consumes a number and produces a
> list according to the above description. For example, if the amount is
> $1.03, then the cash register evaluates (controller 103):
> 
> (controller 103)
> ;; expected value:
> (cons 1 (cons 'dollar (cons 'and (cons 3 (cons 'cents empty)))))
> ---
> (excersise 10.1.9 from
> http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-14.html#node_sec_10.1)

What is the contract for the controller function?
Can we see what you have after you have used the template?

-- 
Jens Axel S�gaard