Question

You must write each of the following scheme functions. You must use only basic scheme functions...

You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects.

This problem need to use DrRacket software. Racket Language.

Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1.

(first-n '(a b c d e f) 3) ---> (a b c)
(first-n '(a b c d e f) -3) ---> ()
(first-n '(a b c d e f) 33) ---> (a b c d e f)
(first-n '() 0) ---> ()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

(define count 1) ;;Initialized count = 1 to keep track our traversal of list

(define (first-n L1 N) ;;MAIN_FUNCTION
(first L1 N count)) ;;calls sub-function Which returns first N-number of elements of list


(define (first L1 N count) ;;SUB_FUNCTION
(cond ((null? L1) L1) ;;if list is empty returns List
((< N 0) '()) ;;if (N < 0) RETURNS NULL LIST
((= N 0) '()) ;;if (N == 0) reurns NULL list
((= N 1) (car L1)) ;;if (N == 1) Returns 1st element
((= N count) (cons (car L1) '())) ;;if (N == count) returns list which contains elelemnt at index N

(else (cons (car L1) (first (cdr L1) N (+ 1 count)))))) ;;else construct list with first element and continue

Snapshot of Code and Output:

File Edit View Language Racket Insert Scripts Tabs Help Untitled (define...) #lang scheme (define count 1) ;; Initialized cou

Add a comment
Know the answer?
Add Answer to:
You must write each of the following scheme functions. You must use only basic scheme functions...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You must write each of the following scheme functions. You must use only basic scheme functions...

    You must write each of the following scheme functions. You must use only basic scheme functions (those described in class), do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (key-store L1 KEY) that accepts a list of two-tuples L1 and a value KEY. This function returns the second value of the first tuple that begins with KEY....

  • Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2...

    Write the function below in scheme/lisp programming language. Drracket Exercise: It is well known that n^2 is equal to the sum of the first n odd numbers. For example, 16 = 4^2 = 7 + 5 + 3 + 1. Write a function that takes as input a natural number, n, and that returns the square of its input by adding the first n odd numbers. Your code may not contain delayed operations and must use accumulative recursion.

  • Use DrRacket to write a Scheme program that takes a list and computes the number of...

    Use DrRacket to write a Scheme program that takes a list and computes the number of atoms in the list. Name your source code num_atoms.rkt. Here are some examples > (num atoms '((a b) c (d (e f)))) 6 > (num_ atoms '(1 2 34 5)) >(num_atoms '((1 2 3 45))) >(num atoms (O))

  • Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or...

    Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or LISP if you prefer) . A function (binomial N k) that returns the binomial coefficients C(N, k), defined recursively as: C(NO) = 1, C(N, N) = 1, and, for 0<k < N E(N, k) = C(N-1, k) + C(N-1, k-1) 2. A function (mod N M) that returns the modulus remainder when dividing N by M 3. A function (binaryToDecimal b) that takes a...

  • Please Provide me all Necessary Screenshot with copy able code. Thanks Write a scheme function named...

    Please Provide me all Necessary Screenshot with copy able code. Thanks Write a scheme function named up-to-first-number that takes a list as its input and returns a list containing all the elements up to the first numeric element in the input list. You can use the number? predicate function to determine whether an element is a number or not. Sample runs: (up-to-first-number '(a b c d 1 2 3 )) returns (a b c d) (up-to-first-number '(d e f 7))...

  • Implement the following Racket functions: (You must use recursion, and not iteration. You may not use...

    Implement the following Racket functions: (You must use recursion, and not iteration. You may not use side-effects (e.g. set!). ) 1. Reflexive? Input: a list of pairs, L and a list S. Interpreting Las a binary relation over the set S, Reflexive? returns #t if L is a reflexive relation over the set S and #f otherwise. Examples: (display "Reflexive?\n") (Reflexive? '((a a) ( bb) (cc)) '(a b c)) ---> #t (Reflexive? '((a a) (bb)) '(a b c)) ---> #f...

  • This is the be anwsered in the language scheme and it only

    this is the be anwsered in the language scheme and it only Question 2 (100 marks in total) Write a recursive function (split L) that takes a list L, and returns a new list containing two sublists with the first sublist containing every second element in L and the second list containing the remaining elements not in the first sublist. Here are examples of how split should work: (split 12345678)) ((1 357) (24 68)) (split (cat bird dog horse mouse...

  • In scheme coding language Write a function filterN that returns only the numbers n through m...

    In scheme coding language Write a function filterN that returns only the numbers n through m from a lat. For example: (filterN 4 6 (1 turkey 5 9 4 bacon 6 cheese)) returns (5 4 6). Use only functions add1, sub1, and, or, <, >, null?, number?, zero?, define, lambda, cond, and else in the code. Assume n < m

  • Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes...

    Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes a list as a parameter and computes the product of all numbers which appear anywhere in the list or in sublists. We will follow the usual mathematical convention that a product of no numbers is 1 (the multiplicative identity). You may find it useful to know that Scheme has a function number? which returns #t if its argument is numeric atom and #f otherwise....

  • Please place all of the following functions (defun …) into a SINGLE .lsp file As such,...

    Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide ​ Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT