Untitled › Forums › Programming Assignment Help › write a lisp function to recursively alphabetize a list
Tagged: programming assignment help
- This topic has 1 reply, 2 voices, and was last updated 2 months ago by
Aakanksha.
-
AuthorPosts
-
January 17, 2013 at 5:09 am #8515
ahwriter
ParticipantI need help with these programs
1) write a parenthesises counter/checker for lisp using snobol
2) write a linked list queue in fortran, write your own new and delete using direct access files instead of arrays
3) write a lisp function to recursively alphabetize a list
August 2, 2023 at 12:12 pm #17270Aakanksha
ParticipantSure, I can provide you with code snippets for each of the tasks you mentioned:
Parentheses Counter/Checker for Lisp using SNOBOL:
SNOBOL is a string manipulation language that can be used to create a parentheses counter/checker for Lisp. Below is a simple example of a SNOBOL program to check if the parentheses are balanced:
DEFINE(‘Stack’, ”)
DEFINE(‘Push’, ‘ -> (A) . Stack’)
DEFINE(‘Pop’, ‘Stack -> (A)’)
DEFINE(‘IsEmpty’, ‘Stack = ”’)DEFINE(‘CheckBalanced’)
‘(‘ Push
‘)’ Pop
:(CheckBalanced)
OUTPUT = ‘Parentheses are balanced’
ENDPATTERN = (‘(‘ | ‘)’)
CheckBalanced
END
Linked List Queue in Fortran with New and Delete using Direct Access Files:
Fortran is not a typical language for implementing data structures like linked lists, but it’s possible. Below is an example of a linked list queue in Fortran using direct access files:
PROGRAM LinkedListQueueIMPLICIT NONE
INTEGER, PARAMETER :: MaxQueueSize = 100
INTEGER :: Front, Rear, Free, NewNode
REAL :: Data(MaxQueueSize)
INTEGER :: NextNode(MaxQueueSize)
CHARACTER(LEN=8), PARAMETER :: FileName = “queue.dat”
LOGICAL :: IsEmpty! Initialize the queue
Front = 0
Rear = 0
Free = 1
Data = 0.0
NextNode = -1! Create an empty queue file
OPEN(UNIT=10, FILE=FileName, FORM=’UNFORMATTED’, ACCESS=’DIRECT’, RECL=Len(Node))
WRITE(10, REC=1) Data, NextNode! Define the Node type
TYPE :: Node
REAL :: Data
INTEGER :: NextNode
END TYPE! New function to allocate a new node
FUNCTION New() RESULT(NewNode)
INTEGER :: NewNode
IF (Free == 0) THEN
PRINT *, “Queue is full. Cannot allocate a new node.”
STOP
END IF
NewNode = Free
Free = NextNode(Free)
END FUNCTION New! Delete function to free a node
SUBROUTINE Delete(NodeToDelete)
INTEGER, INTENT(IN) :: NodeToDelete
NextNode(NodeToDelete) = Free
Free = NodeToDelete
END SUBROUTINE Delete! Function to check if the queue is empty
FUNCTION IsEmpty() RESULT(IsEmpty)
IsEmpty = (Front == 0)
END FUNCTION IsEmpty! Subroutine to add an element to the queue
SUBROUTINE Enqueue(Value)
REAL, INTENT(IN) :: Value
INTEGER :: NewNode
NewNode = New()
Data(NewNode) = Value
NextNode(NewNode) = -1
IF (Rear /= 0) NextNode(Rear) = NewNode
Rear = NewNode
IF (Front == 0) Front = Rear
END SUBROUTINE Enqueue! Function to remove an element from the queue
FUNCTION Dequeue() RESULT(Value)
REAL :: Value
IF (IsEmpty()) THEN
PRINT *, “Queue is empty. Cannot dequeue.”
STOP
END IF
Value = Data(Front)
Front = NextNode(Front)
IF (Front == 0) Rear = 0
END FUNCTION Dequeue! Main program to test the queue
LOGICAL :: QueueEmpty
REAL :: Value! Example usage
Enqueue(10.0)
Enqueue(20.0)
Enqueue(30.0)QueueEmpty = IsEmpty()
IF (.NOT. QueueEmpty) THEN
Value = Dequeue()
PRINT *, “Dequeued value: “, Value
ENDIFQueueEmpty = IsEmpty()
IF (.NOT. QueueEmpty) THEN
Value = Dequeue()
PRINT *, “Dequeued value: “, Value
ENDIF! Close the queue file
CLOSE(10)END PROGRAM LinkedListQueue
Note: Using direct access files in this manner for a linked list queue in Fortran may not be the most efficient or recommended way. Fortran is primarily designed for scientific and numerical computations and does not have built-in support for dynamic data structures like linked lists. For more complex data structures, other languages like C, C++, or Python are generally preferred.Lisp Function to Recursively Alphabetize a List:
Below is an example of a Lisp function to recursively alphabetize a list using the quicksort algorithm:
(defun quicksort (list)
(if (null list)
nil
(let* ((pivot (car list))
(rest (cdr list))
(smaller (quicksort (remove-if (lambda (x) (string-lessp x pivot)) rest)))
(larger (quicksort (remove-if (lambda (x) (string-greaterp x pivot)) rest))))
(append smaller (list pivot) larger))));; Example usage:
(setq my-list ‘(“apple” “orange” “banana” “grape” “kiwi”))
(setq sorted-list (quicksort my-list))
(print sorted-list)
he above Lisp function quicksort uses the remove-if function to split the list into elements smaller and larger than the pivot, and then recursively calls quicksort on those sublists. Finally, it concatenates the smaller, pivot, and larger sublists to get the sorted list.Please note that the above implementations are for illustrative purposes and may not be optimized or suitable for production use. The code provided aims to demonstrate the concepts for each task.
-
AuthorPosts
- You must be logged in to reply to this topic.