Skip to content

Instantly share code, notes, and snippets.

;; P01 (*) Find the last box of a list.
;; Example:
;; * (my-last '(a b c d))
;; (D)
(define (my-last lst)
(if (null? lst)
'()
(let ((tail (cdr lst)))
(if (null? tail)
;; P11 (*) Modified run-length encoding.
;; Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.
;; Example:
;; * (encode-modified '(a a a a b c c a a d e e e e))
;; ((4 A) B (2 C) (2 A) D (4 E))
(require srfi/1)
(define (encode lst)
;; P21 (*) Insert an element at a given position into a list.
;; Example:
;; * (insert-at 'alfa '(a b c d) 2)
;; (A ALFA B C D)
(define (insert-at elm lst pos)
(let loop ((lst lst) (pos pos) (acc '()))
(if (or (= pos 1) (null? lst))
(append (reverse acc) (cons elm lst))
(loop (cdr lst) (- pos 1) (cons (car lst) acc)))))
;; P31 (**) Determine whether a given integer number is prime.
;; Example:
;; * (is-prime 7)
;; T
#lang racket
(provide is-prime?)
(define (is-prime? n)
;; P41 (**) A list of Goldbach compositions.
;; Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
;; Example:
;; * (goldbach-list 9 20)
;; 10 = 3 + 7
;; 12 = 5 + 7
;; 14 = 3 + 11
;; 16 = 3 + 13
;; 18 = 5 + 13
;; 20 = 3 + 17
;; P54A (*) Check whether a given term represents a binary tree
;; Write a predicate istree which returns true if and only if its argument is a list representing a binary tree.
;; Example:
;; * (istree (a (b nil nil) nil))
;; T
;; * (istree (a (b nil nil)))
;; NIL
(define (istree? lst)
(or (null? lst)
;;; ある範囲の数に対しての繰り返しオペレータfor
(define-syntax for
(syntax-rules ()
((_ index start end body ...)
(do ((index start (+ index 1)))
((> index end) #f)
body ...))))
;; 実行例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
### Env クラス
class Env(dict):
"環境: ペア{'var':val} の dict で、外部環境(outer)を持つ。"
def __init__(self, parms = (), args = (), outer = None):
self.update(zip(parms, args))
self.outer = outer
def find(self, var):
#! /usr/bin/env python
import sys,os,math,pygame,random,pprint
bwpx,bhpx,score,bw,bh,board,tickcnt,TICK=0,0,0,10,20,[],0,pygame.USEREVENT + 1
cmap={'A':(255,0,0),'B':(0,255,0),'C':(0,0,255),'D':(255,255,0),'E':(0,255,55),'F':(128,255,0),' ':(0,0,0)}
pieces,piece,px,py=['AAAA',' B \nBBB','CC \n CC',' DD\nDD ','EE\nE \nE ','FF\nFF'],None,0,0
def render():
for i in range(bh):
for j in range(bw):
if i in range(py,py+len(piece.split('\n'))) and j in range(px,px+len(piece.split('\n')[0])):
@cametan001
cametan001 / Lisp.py
Created November 23, 2010 09:34 — forked from wasabili/Lisp.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, traceback
nil = type('Nil', (), {'__str__': lambda self: '()'})()
undef = type('Undef', (), {'__str__': lambda self: '#<undef>'})()
f = type('F', (), {'__str__': lambda self: '#f'})()
t = type('T', (), {'__str__': lambda self: '#t'})()