diff_months: 26

CS 4303 Concept Of Programming Languages Assignment

Download Solution Now
Added on: 2023-09-13 05:37:03
Order Code: Priority
Question Task Id: 0
  • Subject Code :

    CS-4303

  • Country :

    Australia

Purpose:

Introduction to Functional Programming LISP

This document provides instructions about using Lisp and examples of the design of Lisp functions.

TASK 1:

Using the LISP interpreter Download and install Racket software (https://racket-lang.org/download/) on your computer. You may explore the features of the software by trying to run simple lisp functions.

Some of the primitive functions allowed in Lisp are listed below. It would be a very good practice if you try these primitive functions on the system. You may look at the other primitive functions supported by Lisp from any sources.

Please note that Racket, a dialect of Scheme, uses a naming practice that is dramatically different from that of other Lisp dialects, such as Common Lisp, and has a lot of features that are different from those from other Lisp software. It is not surprising if you find some of the language features in the following tasks not available on Racket. If so, find the equivalent feature in Racket, and treat this exercise as a good opportunity to learn Lisp.

EXAMPLE:

LC-USER 1>(car '(a b c))
A
LC-USER 2>(eq 'x 'y)
NIL
LC-USER 3>(setq x '(a b c))
(ABC)

To learn more about LISP programming, click on the following link: http://cms.dt.uh.edu/Faculty/LinH/courses/CS4303/LISP/Lisp_sources.html

TASK 2:

More practice: In each of the following questions predict the results first and then use the Lisp interpreter to check your answers:

  1. Write down the results of typing the following expressions in Lisp. (one line at a time).
    (list 'big 'cat 'sat)
    (cons 'the (list 'big 'cat 'sat))
    (list 'all (list 'good 'people) 'should (list 'go 'ahead))
    (cdr (car (cdr (a (bc) d))))
    (cdadr '(a (b c) d))
  2. What will be the value of the last expression in each case?
    • (setq a '(u v w))
      (set (car (cdr a)) 'b) (cons v a)
    • (setq A'A) (setq B'A)
      (list A B 'B)
  3. What will be the result of each of the following sets of s-expressions typed in Lisp environment?
    • (defun double (x) (2x))
      (double 2.3)
    • (defun times-square (x y) ( x y y))
      (times-square 4 3)
    • (defun TIMES-CUBE (XY) (XYYY))
      (defun CUBE-TIMES (XY) (TIMES-CUBE Y X))
      (CUBE-TIMES 32)

    Using the editor on Racket you may type the above functions in a Racker source file with Using the editor on Racket you may type the above functions in a Racker source file with ".rkt" extension. And then run the program in Racket IDE
    CL-USER 1> (double 5) running the function double 10
  4. Evaluate each of the following s-expressions:
    • (atom '3)
    • (floatp '(AB))
    • (listp '(AB))
    • (not (null 'nil))
    • (not (atom (A B)))
    • (eq 'a 'a)
    • (eq (A B) (list 'A 'B))
    • (eq 3 3.0)
    • (equal 33.0) (eq 2.3 (+ 1.1 1.2))
    • (and (atom 3) (listp (A B))))
  5. Write two different s-expressions to access symbol C for each of the following lists.
    • (ABCDE)
    • ((ABC) (DEF))
    • ((AB) (CD) (EF))
    • (A (BCD) EF)
  6. Write a function DOT-PRODUCT that takes two lists, each list has three numbers (elements), and produces the dot product of the vectors that they represent. For example,
    (DOT-PRODUCT (1.2 2.0-0.2) (0.0 2.3 5.0))
    3.6
    The answer for this question can be found in file DOT-PRODUCT.LSP in the same directory. The key for designing a lisp function is using recursion.
  7. Write a function COUNT-NUMBER that counts the number of numbers that occur in a list. It should work like this:
    32 3/4 >>(COUNT-NUMBER (A 2.3 B 5 4.53 CF))
    ->3
    Hint: To determine whether s-expression is a number or not, use numberp function. The following examples show how numberp works:
    (numberp 5)
    T ->
    (numberp 5.5) >
    T ->
    (numberp T)
    nil >>
    (numberp '(AB))
    -> nil
  8. Write a function NEW-LIST that takes a number as its argument and constructs a list of that length containing all Ts. For example,
    >(new-lit 5)
    >(TTTTT)
  9. The Lisp function LENGTH counts the number of elements in the top level of a list. Write a function ALL-LENGTH of one argument that counts the number of atoms that occur in a list at all levels. Thus, the following lists will have the following ALL-LENGTHS.
    (ABC) =>3
    ((A (BC) D (EF)) =>6
    (NIL NIL (NIL NIL) NIL) => 5
    For each of questions 6-9, test cases are given in the instructions. Write the code for each function, and after each function bundle the test cases together as a block of check-expect invocations.

These tests should illustrate the output you expect, and produce the actual output when running the program. You can write these test cases as executable code using the check- expect primitive. The following are examples:

;; Test Examples:
(check-expect (DOT-PRODUCT (1.2 2.0 -0.2) '(0.0 2.3 5.0)) 3.6)
(check-expect (COUNT-NUMBER '(A 2.3 B 5 4.53 CF)) 3)

When running your program, if the test passes, Racket will display a message telling that all tests have passed. Otherwise, it will show which test cases do not pass.

Turn in your responses to these questions 6-9 in one single Racket source file (.rkt file) and a Word document with snapshots of the test results for the test cases of each function in Canvas by the due date (the beginning of the next class session). Make sure to include all the test cases given in the above lab instructions.

Are you struggling to keep up with the demands of your academic journey? Don't worry, we've got your back! Exam Question Bank is your trusted partner in achieving academic excellence for all kind of technical and non-technical subjects.

Our comprehensive range of academic services is designed to cater to students at every level. Whether you're a high school student, a college undergraduate, or pursuing advanced studies, we have the expertise and resources to support you.

To connect with an expert and ask your query - Book Your Order

  • Uploaded By : Mohit
  • Posted on : September 13th, 2023
  • Downloads : 0
  • Views : 427

Download Solution Now

Can't find what you're looking for?

Whatsapp Tap to ChatGet instant assistance

Choose a Plan

Premium

80 USD
  • All in Gold, plus:
  • 30-minute live one-to-one session with an expert
    • Understanding Marking Rubric
    • Understanding task requirements
    • Structuring & Formatting
    • Referencing & Citing
Most
Popular

Gold

30 50 USD
  • Get the Full Used Solution
    (Solution is already submitted and 100% plagiarised.
    Can only be used for reference purposes)
Save 33%

Silver

20 USD
  • Journals
  • Peer-Reviewed Articles
  • Books
  • Various other Data Sources – ProQuest, Informit, Scopus, Academic Search Complete, EBSCO, Exerpta Medica Database, and more