Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,45 @@
PROGRAM CLASS_DEMO
CLASS QUEUE
LOCAL SP
LOCAL DIM STACK[100]
FUNCTION ISEMPTY()
ISEMPTY=(SP=0)
END FUNCTION
PROCEDURE INIT
SP=0
END PROCEDURE
PROCEDURE POP(->XX)
XX=STACK[SP]
SP=SP-1
END PROCEDURE
PROCEDURE PUSH(XX)
SP=SP+1
STACK[SP]=XX
END PROCEDURE
END CLASS
NEW PILA:QUEUE
BEGIN
PILA_INIT ! constructor
FOR N=1 TO 4 DO ! push 4 numbers
PRINT("Push";N)
PILA_PUSH(N)
END FOR
FOR I=1 TO 5 DO ! pop 5 numbers
IF NOT PILA_ISEMPTY() THEN
PILA_POP(->N)
PRINT("Pop";N)
ELSE
PRINT("Queue is empty!")
END IF
END FOR
PRINT("* End *")
END PROGRAM

View file

@ -0,0 +1,35 @@
;; put info string in permanent storage for later use
(info 'make-Q
"usage: (define q (make-Q)) ; (q '[top | empty? | pop | push value | to-list | from-list list])")
;; make-Q
(define (make-Q)
(let ((q (make-vector 0)))
(lambda (message . args)
(case message
((empty?) (vector-empty? q))
((top) (if (vector-empty? q) (error 'Q:top:empty q) (vector-ref q 0)))
((push) (vector-push q (car args)))
((pop) (if (vector-empty? q) (error 'Q:pop:empty q) (vector-shift q)))
((to-list) (vector->list q))
((from-list) (set! q (list->vector (car args))) q )
(else (info 'make-Q) (error "Q:bad message:" message )))))) ; display info if unknown message
;;
(define q (make-Q))
(q 'empty?) → #t
(q 'push 'first) → first
(q 'push 'second) → second
(q 'pop) → first
(q 'pop) → second
(q 'top)
"💬 error: Q:top:empty #()"
(q 'from-list '( 6 7 8)) → #( 6 7 8)
(q 'top) → 6
(q 'pop) → 6
(q 'to-list)→ (7 8)
(q 'delete)
"💭 error: Q:bad message: delete"
;; save make-Q
(local-put 'make-Q)

View file

@ -0,0 +1,21 @@
define myqueue => type {
data store = list
public onCreate(...) => {
if(void != #rest) => {
with item in #rest do .`store`->insert(#item)
}
}
public push(value) => .`store`->insertLast(#value)
public pop => {
handle => {
.`store`->removefirst
}
return .`store`->first
}
public isEmpty => (.`store`->size == 0)
}

View file

@ -0,0 +1,14 @@
local(q) = myqueue('a')
#q->isEmpty
// => false
#q->push('b')
#q->pop
// => a
#q->pop
// => b
#q->isEmpty
// => true
#q->pop
// => void

View file

@ -0,0 +1,18 @@
import queues
# defining push & pop (obviously optional)
proc push*[T](q: var TQueue[T]; item: T) =
add(q,item)
proc pop*[T](q: var TQueue[T]): T =
result = dequeue(q)
var fifo: TQueue[int] = initQueue[int]()
fifo.push(26)
fifo.push(99)
fifo.push(2)
echo("Fifo size: ", fifo.len())
echo("Popping: ", fifo.pop())
echo("Popping: ", fifo.pop())
echo("Popping: ", fifo.pop())
#echo("Popping: ", fifo.pop()) # popping an empty stack raises [EAssertionFailed]

View file

@ -0,0 +1,6 @@
Object Class new: Queue(mutable l)
Queue method: initialize ListBuffer new := l ;
Queue method: empty @l isEmpty ;
Queue method: push @l add ;
Queue method: pop @l removeFirst ;

View file

@ -0,0 +1,15 @@
sequence queue = {}
procedure push(object what)
queue = append(queue,what)
end procedure
function pop()
object what = queue[1]
queue = queue[2..$]
return what
end function
function empty()
return length(queue)=0
end function

View file

@ -0,0 +1,13 @@
class FIFO(*array) {
method pop {
array.is_empty && die "underflow";
array.shift;
}
method push(*items) {
array += items;
self;
}
method empty {
array.len == 0;
}
}

View file

@ -0,0 +1,20 @@
def (queue seq)
(tag queue (list seq lastcons.seq len.seq))
def (enq x q)
do1 x
let (l last len) rep.q
rep.q.2 <- (len + 1)
if no.l
rep.q.1 <- (rep.q.0 <- list.x)
rep.q.1 <- (cdr.last <- list.x)
def (deq q)
let (l last len) rep.q
ret ans car.l
unless zero?.len
rep.q.2 <- (len - 1)
rep.q.0 <- cdr.l
def (len q) :case (isa queue q)
rep.q.2

View file

@ -0,0 +1,17 @@
(define-class queue
(instance-variables vals))
(define-method (queue 'initialize)
(setq vals '())
self)
(define-method (queue 'push x)
(setq vals (nconc vals (cons x nil))))
(define-method (queue 'pop)
(define val (car vals))
(setq vals (cdr vals))
val)
(define-method (queue 'emptyp)
(null vals))

View file

@ -0,0 +1,24 @@
[1] (define my-queue (queue 'new))
MY-QUEUE
[2] (my-queue 'push 1)
(1)
[3] (my-queue 'push 2)
(1 2)
[4] (my-queue 'emptyp)
()
[5] (my-queue 'pop)
1
[6] (my-queue 'pop)
2
[7] (my-queue 'emptyp)
#T
[8] (my-queue 'pop)
()

View file

@ -0,0 +1,10 @@
# An empty queue:
def fifo: [];
def push(e): [e] + .;
def pop: [.[0], .[1:]];
def pop_or_error: if length == 0 then error("pop_or_error") else pop end;
def empty: length == 0;

View file

@ -0,0 +1,13 @@
fifo | pop # produces [null,[]]
fifo
| push(42) # enqueue
| push(43) # enqueue
| pop # dequeue
| .[0] # the value
# produces 43
fifo|push(1) as $q1
| fifo|push(2) as $q2
| [($q1|pop|.[0]), ($q2|pop|.[0])]
# produces: [1, 2]