-
This presentation is the property of its rightful owner.
Sponsored Links
1 / 49

תכנות פונקציונאלי - II PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on
  • Presentation posted in: General

תכנות פונקציונאלי - II. רקורסיה מבוסס על: לירון, א., לפידות ת., לוי, ד. ופז, ת. (1999). תכנות פונקציונאלי – פרדיגמה תכנותית נוספת , המרכז הישראלי להוראת המדעים. תכנות פונקציונאלי - תיאורים רקורסיביים. להלן שלושה תיאורים רקורסיביים. לכל תיאור, ציירו את הציור המתאים לרמה 3.

Download Presentation

תכנות פונקציונאלי - II

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Ii

- II

:, ., ., , . , . (1999). , .


Ii

-

. , 3.

  • N N-1.

  • 1, .

  • N V N-1.

  • 1, V.

  • N N-1.

  • 1, .


Ii

-

  • . , .

    • , . ().

    • ' ', . ( ).


Ii

-

  • . , .

    • A , B, . C.

    • A, B, . C .


Ii

.


Ii

- :

  • .

  • .

    • .

  • , , , , .


Ii

- :

  • - :

    • () , .

    • , , () ().


Ii

-

  • .

    • .

    • .


Ii

-

)define (sum_list l(

(cond

[(empty? l) 0[

[(+ (first l) (sum_list (rest l)))]))


Ii

-

  • .

    • .

    • .


Ii

-

)define (sum_even l)

(cond

[(empty? l) 0]

[(even? (first l)) (+ (first l) (sum_even (rest l)))]

[else (sum_even (rest l))]))


Ii

-

  • :

    • (10 20 30 40) , (10 30 40).

    • (what a nice day) , (what a day).

  • : i - 0 , - i-.


Ii

-

.

  • without-1 .

    • : (without-1 ( 10 20 30 40 ) ) (20 30 40).

      (define (without_1 l)

      (rest l))


Ii

-

  • without-2 .

    • , (without-2 ( 10 20 30 40 ) ) (10 30 40).

      (define (without_2 l)

      (cons (first l)(without_1 (rest l))))


Ii

-

  • without-3?

  • (10 20 30 40 50 60).

  • (without_1 (10 20 30 40 50 60)) ( 20 30 40 50 60)

  • (without_2 (10 20 30 40 50 60)) ( 10 30 40 50 60)

  • .

  • (without_3 (10 20 30 40 50 60)) ( 10 20 40 50 60 )


Ii

-

  • , .

    (define (without_3 l)

    (cons (first l)(without_2 (rest l))))

  • .


Ii

-

  • ?

  • without?


Ii

-

  • without L n.

    (define (without n L)

    (cond

    [(= n 1) (rest L)]

    [else (cons (first L) (without (sub1 n) (rest L)))]))

  • (= n 1) ?

  • (rest L)?


Ii

- ,

  • upto (" -" ): L n n ( - n, ).

    > (upto 3 ( alon gila roni miri moti yael ))

    (alon gila roni )

    >(upto 2 ( 10 (2.5 3.5) x hello ))

    (10 (2.5 3.5 ) )

  • :

    (define (upto n l)

    (cond

    [(= n 1)(cons (first l) '())]

    [else (cons (first l) (upto (sub1 n)(rest l)))]))


Ii

- ,

  • from ( "-" ): L n -n .

    >(from 3 ( alon gila roni miri moti yael))

    (roni miri moti yael )

    >(from 2 ( 10 (2.5 3.5) x hello))

    ((2.5 3.5) x hello)

    (define (from n l)

    (cond

    [(= n 1) l]

    [else (rest (from (sub1 n) l))]))


Ii

- ,

  • from, upto .

  • - (between L a b), L a b.

    >(between ( alon gila roni miri moti yael ) 3 5)

    (roni miri moti )

    >( between ( 10 (2.5 3.5) x hello ) 2 3 )

    ( ( 2.5 3.5 ) x )

  • between .


Ii

- ,

  • (between L a b) L a b:

    (define (between l a b)

    (upto (add1 (- b a)) (from a l)))

    > (between '(1 2 3 4 5 6 7) 2 4)

    (2 3 4)

    > (between '(1 2 3 4 5 6 7) 2 7)

    (2 3 4 5 6 7)

    > (between '(1 2 3 4 5 6 7) 4 4)

    (4)


Ii

- ,

  • (between L a b) L a b:

    (define (between l a b)

    (upto (add1 (- b a)) (from a l)))

    :

    > (between '(1 2 3 4 5 6 7) 1 8)

    > (between '(1 2 3 4 5 6 7) 1 7)

    > (between '(1 2 3 4 5 6 7) 3 2)


Ii

- ,

  • from - upto (insert L x n), x - n L.

  • :

  • (insert (alon dalya roni miri) gadi 3)

    • (alon dalya gadi roni miri)

  • (insert (10 (2.5 3.5) x hello) (a b c) 2)

    • (10 (a b c) (2.5 3.5) x hello)

  • insert , from- .upto


Ii

-

  • :

    • :

        • -


Ii

- , (map)

  • .

    • (100 64 0 25 1)

      (10 8 0 5 1 ).

  • .

    • ((my cat) (good morning) (dad and mom))

      (my good dad).


Ii

- , (map)

  • .

    (define (sqrt_list l)

    (cond

    [(empty? l) l]

    [else (cons (sqrt (first l)) (sqrt_list (rest l)))]))


Ii

- , (map)

  • .

    (define (first_list l)

    (cond

    [(empty? l) l]

    [else (cons (first (first l)) (first_list (rest l)))]))


Ii

- , (map)

  • " ( ) ( ).

  • .

  • .


Ii

- , (map)

  • .


Ii

- , (map)

  • MAP .

    )define (map list operation)

    (cond

    [(empty? list ) '()]

    [else (cons (operation (first list)) (map (rest list) operation))]))

    > (map '(8 7 6) add1)

    (9 8 7)

    > (map '(16 0 6) sqrt)

    (4 0 2.4494897427832)


Filter

- , (filter)

  • .

    • .

    • .


Filter1

- , (filter)

  • .

    )define (only_numbers l)

    (cond

    [(empty? l ['() (

    [(number? (first l))

    (cons (first l) (only_numbers (rest l [(((

    [else (only_numbers (rest l))]))


Filter2

- , (filter)

  • .

    (define (sod L)

    (cond

    [ ( empty? L ) ( ) ]

    [ (negative? (first L ) ) ( sod ( rest L ) ) ]

    [ else (cons ( sqrt (first L)) (sod (rest L )))]))

  • ?

    (64 -4 100 25 -25 )

    (1 49 -9 -16 81 4 )

  • ?

  • .


Filter3

- , (filter)

  • .

    (define (not_list l)

    (cond

    [(empty? l) '()]

    [(list? (first l)) (not_list (rest l))]

    [else (cons (first l) (not_list (rest l)))]))


Filter4

- , (filter)

  • " ( ) ( ) .

  • .

  • .

  • filter ( ?)


Filter5

- , (filter)

.

(define (filter l predicate)

(cond

[(empty? l) '()]

[(predicate (first l))

(cons (first l) (filter (rest l) predicate))]

[else (filter (rest l) predicate)]))


Filter6

- , (filter)

  • .

  • filter - 6.

    (define (divide_6? x)

    (= (reminder x 6) 0))

    > (divide_6? 8(

    #f

    > )divide_6? 12(

    #t

    >) filter '(4 6 12 67 60 180) divide_6(?

    (6 12 60 180)


Reduce

- , (reduce)

  • .

  • :

    • #t - #f .

    • #t - #f .


Reduce1

- , (reduce)

  • " .

  • reduce.

    • ?

    • ?


Map filter reduce

- map, filter, reduce

  • first order objects


Ii

  • :

    • .


Ii

  • , ( )

  • : .

  • :

    • .

    • .

  • : .


Ii

  • .

    )define (sup-big-than-average l)

    (big-than-average l (average l)))

    )define (big-than-average l average)

    (cond

    [(empty? l) 0 ]

    ](> (first l) average)

    (add1 (big-than-average (rest l) average))]

    ]else (big-than-average (rest l) average)]))


Ii

  • .

    • - - .


Ii

, ,

  • . , - .

    :

  • ( ) .

  • ( ).

  • .

  • "" .

    • , () .


Ii

, ,

:

  • (tail recursion) - ().

  • () - .

  • (-) .

  • (-) .


Ii

  • :

      • -


  • Ii

    • ?


  • Login