2010년 11월 11일 목요일

[sicp] 1.17, 1.18

#lang scheme
(define (even? n)
  (= (remainder n 2) 0))

(define (double a )
   (double-iter a 2 0))

(define (double-iter a counter product)
  (if (= counter 0)
      product
      (double-iter a (- counter 1) (+ a product))))


(define (mod a div)
  (- (/ a div) (/ 1 div)))

;; 1.18
(define (fast-multi-iter a counter product result )
  (cond ((= counter 1) (+ product  result ) )
        (( even? counter) (fast-multi-iter a (/ counter 2)  (double product) result ))
        (else (fast-multi-iter a (mod counter 2) (double product) (+ result product ) ) )))
 
(define (* a b)
  (fast-multi-iter a b a 0 )
  )

 

 

 

;; 1.17
(define (fast-multi b n)
  (cond ((= n 0) 0)
        ((even? n) (double (fast-multi b (/ n 2))))
        (else (+ b (fast-multi b (- n 1))))))

 

ps. 1.18 을 답을 보니 나만 틀렸네 ㅡㅡ

잘못된 방법으로 풀었음 .

역시 난 ㅠㅠ

영어 어려워서 책 질렀음

2010년 11월 5일 금요일

[sicp] 1.16

;;; sicp 1.16
#lang scheme
(define (square x) (* x x))

(define (even? n)
  (= (remainder n 2) 0))

(define (fast-expt-iter b counter product)
  (if (= counter 0)
      product
     (if (even? counter)
         (fast-expt-iter b
                (/ counter 2)
                (* b  product product))
         (fast-expt-iter b
                (- counter 1)
                (* b product))
          )
         ))
(define (fast-expt b n)
 (fast-expt-iter b n 1))

(fast-expt 2 16)

 

이것도 들렸다 ㅠㅠ

2010년 10월 22일 금요일

[flex] 메뉴 간격 줄이기

http://blog.flexexamples.com/2010/02/19/setting-a-variable-row-height-on-an-mx-menubar-control-in-flex/comment-page-1/#comment-8471

 

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/19/setting-a-variable-row-height-on-an-mx-menubar-control-in-flex/ -->
<mx:Application name="MenuBar_menuShow_menu_variableRowHeight_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        backgroundColor="white">
 
    <mx:Script>
        <![CDATA[
            import mx.events.MenuEvent;
 
            protected function mBar_menuShowHandler(evt:MenuEvent):void {
                evt.menu.variableRowHeight = ch.selected;
                evt.menu.invalidateSize();
            }
        ]]>
    </mx:Script>
 
    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="ch" label="variableRowHeight" />
    </mx:ApplicationControlBar>
 
    <mx:MenuBar id="mBar"
            labelField="@label"
            menuShow="mBar_menuShowHandler(event);">
        <mx:dataProvider>
            <mx:XMLListCollection>
                <mx:XMLList xmlns="">
                    <menu label="File...">
                        <item label="New" />
                        <item label="Open" />
                        <item label="Save" />
                        <item label="Save As" />
                        <fake type="separator" />
                        <item label="Exit" />
                    </menu>
                    <menu label="Edit...">
                        <item label="Cut" />
                        <item label="Copy" />
                        <item label="Paste" />
                        <fake type="separator" />
                        <item label="Undo" />
                        <item label="Redo" />
                        <fake type="separator" />
                        <item label="radio button" type="radio" toggled="true" />
                        <item label="check box" type="check" toggled="true" />
                    </menu>
                </mx:XMLList>
            </mx:XMLListCollection>
        </mx:dataProvider>
    </mx:MenuBar>
 
</mx:Application>

2010년 10월 1일 금요일

[ActionScript] addEventListener시 파라미터 추가

현재 상황

  • 화면상에 여러개의 버튼과 textfield를 추가
  • script로 for문을 돌려서 실행하였고 그걸 다시 line 별로 같은 line의 textfield값의 받아서 처리 해야함
  • script에서 new로 button을 생성하다보니.. addEventListener 처리를 하다보니 받을 수 있는 값은 event 밖에 없음

처리 방법

  • button 상속후 버튼에 사용할 propeties 정의 setter/getter 추가
  • script에서 for문 이용해서 button 생성시 id값 넘겨줌
  • event 처리부에서 event.currentTarget.새로정의한properties 값 받아서 처리

[code]package custom
{
 import mx.controls.Button;  public class ExtendButton extends Button
 {
 
  private var _currentId:String;  public function ExtendButton()
  {
   super();
  }
 
  public function get currentId():String {
   return _currentId;
  }
  public function set currentId(currentId:String):void {
   _currentId = currentId;
  }
 
 
 }
}[/code]

 

위 처러 만든 후에

[code]var button:ExtendButton = new ExtendButton();
 for(var i:int=1;i<10;i++) {
  button.currentId = "a"+i;
 }
 
 button.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
  trace(event.currentTarget.currentId)
 });[/code]

 

이렇게 만들었음

 

------------------------------------------------------------------------------

여기서 부터는 질문

 http://blog.flashplatform.kr/tag/keep-generated-actionscript

 

위의 링크방식대로 한다고 하면

 

generated라고 옵션은 줘서 as 스크립트를 전부 만들어서 거기에서 추가적으로

 

처리를 해야지 처리가 가능한거지?? ㅡㅡ;;

 

아니면 안되는건지 알고 싶습니다.

 

 

2010년 9월 30일 목요일

[sicp] 1.13

Exercise 1.13.  Prove that Fib(n) is the closest integer to n/5, where = (1 + 5)/2. Hint: Let = (1 - 5)/2. Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that Fib(n) = (n - n)/5.
0 1 1 2 3 5 8
fin n = 1  ((1+ 5)/2 - (1-5)/2 )/5 => 5/5 => 1
fin n = 2  (((1+ 5)/2)^2 - ((1-5)/2 )^2)/5
=> ( (1+ 5)/2 - (1-5)/2 )( (1+ 5)/2 + (1-5)/2 ) ) /5
=> 5/5 => 1

#lang scheme
(define (fib n)
  (fib-iter 1 0 n))

(define (fib-iter a b count)
  (if (= count 0)
      b
      (fib-iter (+ a b) a (- count 1))))

(define A
 (/   (+ 1 (expt 5 0.5))
     2))

(define B
 (/   (- 1 (expt 5 0.5))
     2))

(define (fib2 n)
 (/ (- (expt A n)
    (expt B n))
   (expt 5 0.5)))

(fib2 1) = 1.0
(fib 1) = 1
(fib2 2) = 1.0
(fib 2) = 1
(fib2 3) = 2.0
(fib 3) = 2
...
(fib2 40) = 102334155.00000013
(fib 40) = 102334155
(fib2 50) = 12586269025.00002
(fib 50) = 12586269025
...
(fib2 60) = 1548008755920.003
(fib 60) =1548008755920
(fib2 70) = 190392490709135.44
(fib 70) = 190392490709135

(fib2 80) = 23416728348467744.0
(fib 80) = 23416728348467685

70까지는 거의 근접하고 1~ 3까지는 확실히 똑같다. 3 이후부터는 소수점이 많이 늘어난다.

2010년 9월 29일 수요일

[sicp] 1.12

Exercise 1.12.  The following pattern of numbers is called Pascal's triangle.

The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 Write a procedure that computes elements of Pascal's triangle by means of a recursive process.


#lang scheme
(define (pascal_triangle a b )
  (cond
       ((= a b) 1)
        ((= a 2) 1)
         ((= b 1) 1)
       (else (+ (pascal_triangle (- a 1) (- b 2))
               (pascal_triangle (- a 1) (- b 1))))))


--------------------------------------------------------------------------------

;;; sicp 1.12
#lang scheme
(define (pascal_triangle a b )
  (cond
       ((< a b) '틀렸음)
       ((= a b) 1)
         ((= b 1) 1)
       (else (+ (pascal_triangle (- a 1) (- b 1))
               (pascal_triangle (- a 1) b)))))

 

수정함


[sicp] 1.11

Exercise 1.11.  A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.

- recursive process
#lang scheme

(define (f n)
  (cond ((= n 0) 0)
       ((= n 1) 1)
       ((= n 2) 2)
       (else (+ (f (- n 1))
               (* 2 (f (- n 2)))
               (* 3 (f (- n 3)))) ) ) )

- iterative process
#lang scheme

(define (f-iter a b c count)
   (if (= count 0)
     c
        (f-iter (+ a (* 2 b) (* 3 c) )
               a   b  (- count 1) ) ) )
    
(define (f n )
(f-iter 2 1 0 n) )