Racket, writing function that find nth element in list

racket remove element from list
racket reverse list
racket length of list
racket get nth element of list
racket split list
racket declare list
map scheme
how to go through a list in racket

In order to understand functional programing, please help me to write a function that output nth element of a list,

Allowed command:

define  lambda  cond  else  empty  empty?  first  rest  cons  list

list?  =  equal?  and  or  not  +  -  *  /  <  <=  >  >=

Sample output:

(fourth-element '(a b c d e)) => d

(fourth-element '(x (y z) w h j)) => h

(fourth-element '((a b) (c d) (e f) (g h) (i j))) => (list 'g 'h) or ‘(g h)

(fourth-element '(a b c)) => empty

I could write this in python, but I am not family with racket syntax,

def element(lst, x=0):
  counter = x;
  if (counter >= 3):
    return lst[0]
  else:
    return element(lst[1:],x+1)

a = [1,2,3,4,5,6]
print(element(a))

The Output is 4 Comparing with code above in python. What is equivalent behavior in function that create local variable counter. What is "keyword" for return

2.3 Lists, Iteration, and Recursion, The map function uses the per-element results to create a new list: You can write equivalent iterations using a handful of list primitives. Since a Racket list is a linked for “construct.” To get an empty list to start with, use the empty constant:  Functional version of deleting nth element in a list in Racket. I want to get a list which has nth version deleted from the original list.

Here is how to write it in Python:

def nth(lst, idx=0):
  if (len(lst) == 0):
    return "empty"
  elif (idx == 0):
    return lst[0]
  else:
    return nth(lst[1:], idx - 1)

nth([1,2,3], 1)
# ==> 2

def fourth-element(lst):
  return nth(lst, 4)

Same in Scheme/Racket:

(define (nth lst idx)
  (cond ((empty? lst) empty)  ; more effiecent than (= (length lst) 0)
        ((= idx 0) (first lst))
        (else (nth (rest lst) (- idx 1))))

(nth '(1 2 3) 1)
; ==> 2

(define (fourth-element lst)
  (nth lst 4))

There is no keyword for return. Every form returns the last evaluated code:

(if (< 4 x)
    (bar x)
    (begin 
      (display "print this")
      (foo x)))

This if returns either the result of (bar x) or it prints "print this" then returns the result of (foo x). The reason is that for the two outcomes of the if they are the tail expressions.

(define (test x)
  (+ x 5)
  (- x 3))

This function has two expressions. The first is dead code since it has no side effect and since it's not a tail expression, but the (- x 3) is what this function returns.

(define (test x y)
  (define xs (square x))
  (define ys (square y))
  (sqrt (+ xs ys)))

This has 3 expressions. The first two has side effects that it binds two local variables while the third uses this to compute the returned value.

4.9 Pairs and Lists, Returns #t if v is a list: either the empty list, or a pair whose second element is a list. The andmap function is actually closer to foldl than map, since andmap  At that point, you can start exploiting the language and software design to interesting questions, like getting Yahooo to tell you which maps to download from Google ad display locally -- via a Racket program.

(define a `(1 2 3 (4 5) 7))
(define b `(1 2 3))
(define c `((a b)(c d)(e f)(g h)(i j)))
(define d `(a b c))

(define (my-lst-ref lst counter)
  (cond[(>= counter 3) (first lst)]
       [else (my-lst-ref (rest lst)(+ counter 1))]
       )
  )

(define (fourth-element lst)
  (cond[(>= (list-length lst) 4) (my-lst-ref lst 0)]
       [else "empty"]))

(fourth-element a)
(fourth-element c)
(fourth-element d)

Output:

(list 4 5) (list 'g 'h) "empty"

Lists, Any time you put values between parentheses, you're making a list. 1 2. (list 1 2 3 (list (list 'a 'b 'c) 4 5 6)) '(1 2 3 ((a b c) 4 5 6)) ; quote is applied to each element a list of multiple values, use unquote-splicing, which is usually written as a map: apply a function to every value in a list, and get back a list of return values. Sean Eshbaugh: Web Developer + Programmer. Common Lisp and Clojure both provide a built-in nth function for retrieving the nth element in a list. Surprisingly enough Scheme (MIT Scheme at least) doesn't that I'm aware of.

Nth Element of a List in Scheme, Common Lisp and Clojure both provide a built-in nth function for retrieving the nth element in a list. Surprisingly enough Scheme (MIT Scheme  In addition to simple operations like append, Racket includes functions that iterate over the elements of a list. These iteration functions play a role similar to for in Java, Racket, and other languages.

Lecture 5: Functions on Lists, Higher Order Functions, The select_set function lets you choose members out of a list We also write our own version of member, member? . These built-in The append function always works on lists, and combines the elements of the lists at the same level. A call  Also note that if you find yourself doing random access through the list, then it's probably not the appropriate container type for your data. Racket (as well as Scheme) provides the vector type that supports constant-time random access.

Introduction to Functional Programming in Scheme, Racket is probably the Scheme system in most widespread use today. Earlier Write a function, every-second-element , that returns every second element of a list. (define (every-nth-element n lst) (if (shorter-than? n lst) (list (car lst)) (cons (​car lst) Like assoc , get-prop should return #f if key is not found in property-list . Searches the heads of a list of lists l, and if it finds tgt , returns a new list with the tail of tgt substituted for new. Otherwise, returns False. For clarity’s sake, the following examples may be more illustrative of the difference between subst and subst*.

Comments
  • Nobody is going to do your homework for you. We can help, but then we need a specific problem and you need to show effort. And of course, if you don't show code the answers won't show code.
  • @Sylwester Can you refer me some code example in racket because reading document does not help me much. I need sample code to start. Here I try to start to write a recursive function, but I create local variable and racket gives me error that a local variable is not top-level- variable
  • Very Specific. Thank you very Sir/Madam.
  • While cdr always return a list assoc is an example were it doesn't. It returns (or/c pair? #f). Consistent return is not a typical Scheme feature, but up to the designer which isn\t OP but a professor so while I like the idea of failure (I even assume right input and let cdr fail) one cannot do that when implementing a predefined contract. I would agree OP could do this if the examples didn't define the empty outcome.
  • "... cdr always return a list" - but (cdr (cons 1 2)) returns 2. I agree with everything else you're saying :D
  • Thank you. Very helpful for rookie in Racket.