TopCoder

User's AC Ratio

58.3% (7/12)

Submission's AC Ratio

41.4% (29/70)

Tags

Description

define 古可魚 Base
題目敘述請按此

Input Format

輸入檔中僅有一組測試資料,為以Base語法寫成的若干句話。保證每句話均符合Base語之文法。 每句話可能涵蓋一行或多行,但同一行中只會有一句話。如果遇到以 “;” (不含雙引號) 開頭的行,那是考古學家的註記,請不要翻譯。註記不會夾雜在Base語中。
保證所有數字在運算過程中絕對值不超過 230 。過程中翻譯詞句的總次數不會超過 107 個。

Output Format

印出所有翻譯過程中會印出的東西,一個一行。遇到考古學家的註記的時候依原樣直接輸出即可。

Sample Input 1

; 3
(display (+ 1 2))

(define a 3)
; 7
(display (+ a 4))

(define add1 (lambda (x) (+ x 1)))
; 6
(display (add1 5))

(define pair (lambda (x y) (lambda (m) (m x y))))
(define first (lambda (x) (x (lambda (a b) a))))
(define second (lambda (x) (x (lambda (a b) b))))

(define a (pair 1 2))
; 1
(display (first a))
; 2
(display (second a))

(define [] (pair 1 1))
(define empty? first)
(define : (lambda (hd tl) (pair 0 (pair hd tl))))
(define head (lambda (xs) (first (second xs))))
(define tail (lambda (xs) (second (second xs))))

(define ++
(lambda (xs ys)
(if (empty? xs)
ys
(: (head xs) (++ (tail xs) ys)))))

(define map
(lambda (func xs)
(if (empty? xs)
[]
(: (func (head xs)) (map func (tail xs))))))

(define display-list (lambda (xs) (map display xs)))

(define peter (: 5 (: 0 (: 2 (: 1 (: 6 []))))))
; 5 0 2 1 6
(display-list peter)

(define filter
(lambda (pred xs)
(if (empty? xs)
[]
(begin
(define xs' (filter pred (tail xs)))
(if (pred (head xs))
(: (head xs) xs')
xs')))))

(define ! (lambda (x) (if x 0 1)))
(define <= (lambda (x y) (! (< y x))))
(define > (lambda (x y) (< y x)))
(define sort
(lambda (xs)
(if (empty? xs)
[]
(begin
(define x (head xs))
(define xs' (tail xs))
(define <=x (filter (lambda (y) (<= y x)) xs'))
(define >x (filter (lambda (y) ( > y x)) xs'))
(++ (sort <=x) (: x (sort >x)))))))

; 0 1 2 5 6
(display-list (sort peter))

(define take
(lambda (xs)
(lambda (n)
(if n
(: (head xs) ((take (tail xs)) (- n 1)))
[]))))

; 5 0 2
(display-list ((take peter) 3))
(define take-peter (take peter))
; 5 0 2
(display-list (take-peter 3))

(define yin-yang
(lambda ()
(lambda (m)
(m 0 (pair 0 (lambda (m)
(m 0 (pair 1 (yin-yang)))))))))

; 0 1 0 1 0
(display-list ((take (yin-yang)) 5))

Sample Output 1

; Please read the problem statment carefully :)
; 3
3
; 7
7
; 6
6
; 1
1
; 2
2
; 5 0 2 1 6
5
0
2
1
6
; 0 1 2 5 6
0
1
2
5
6
; 5 0 2
5
0
2
; 5 0 2
5
0
2
; 0 1 0 1 0
0
1
0
1
0

Hints

Problem Source

NPSC 2013

Subtasks

No. Testdata Range Score
1 0 100

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 8000 131072 262144 1