tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
16
Task/Queue-Definition/Haskell/queue-definition-1.hs
Normal file
16
Task/Queue-Definition/Haskell/queue-definition-1.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
data Fifo a = F [a] [a]
|
||||
|
||||
emptyFifo :: Fifo a
|
||||
emptyFifo = F [] []
|
||||
|
||||
push :: Fifo a -> a -> Fifo a
|
||||
push (F input output) item = F (item:input) output
|
||||
|
||||
pop :: Fifo a -> (Maybe a, Fifo a)
|
||||
pop (F input (item:output)) = (Just item, F input output)
|
||||
pop (F [] [] ) = (Nothing, F [] [])
|
||||
pop (F input [] ) = pop (F [] (reverse input))
|
||||
|
||||
isEmpty :: Fifo a -> Bool
|
||||
isEmpty (F [] []) = True
|
||||
isEmpty _ = False
|
||||
34
Task/Queue-Definition/Haskell/queue-definition-2.hs
Normal file
34
Task/Queue-Definition/Haskell/queue-definition-2.hs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# Use a record to hold a Queue, using a list as the concrete implementation
|
||||
record Queue(items)
|
||||
|
||||
procedure make_queue ()
|
||||
return Queue ([])
|
||||
end
|
||||
|
||||
procedure queue_push (queue, item)
|
||||
put (queue.items, item)
|
||||
end
|
||||
|
||||
# if the queue is empty, this will 'fail' and return nothing
|
||||
procedure queue_pop (queue)
|
||||
return pop (queue.items)
|
||||
end
|
||||
|
||||
procedure queue_empty (queue)
|
||||
return *queue.items = 0
|
||||
end
|
||||
|
||||
# procedure to test class
|
||||
procedure main ()
|
||||
queue := make_queue()
|
||||
|
||||
# add the numbers 1 to 5
|
||||
every (item := 1 to 5) do
|
||||
queue_push (queue, item)
|
||||
|
||||
# pop them in the added order, and show a message when queue is empty
|
||||
every (1 to 6) do {
|
||||
write ("Popped value: " || queue_pop (queue))
|
||||
if (queue_empty (queue)) then write ("empty queue")
|
||||
}
|
||||
end
|
||||
30
Task/Queue-Definition/Haskell/queue-definition-3.hs
Normal file
30
Task/Queue-Definition/Haskell/queue-definition-3.hs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Use a class to hold a Queue, with a list as the concrete implementation
|
||||
class Queue (items)
|
||||
method push (item)
|
||||
put (items, item)
|
||||
end
|
||||
|
||||
# if the queue is empty, this will 'fail' and return nothing
|
||||
method take ()
|
||||
return pop (items)
|
||||
end
|
||||
|
||||
method is_empty ()
|
||||
return *items = 0
|
||||
end
|
||||
|
||||
initially () # initialises the field on creating an instance
|
||||
items := []
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
queue := Queue ()
|
||||
|
||||
every (item := 1 to 5) do
|
||||
queue.push (item)
|
||||
|
||||
every (1 to 6) do {
|
||||
write ("Popped value: " || queue.take ())
|
||||
if queue.is_empty () then write ("empty queue")
|
||||
}
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue