DEV Community

Abdullah Gira
Abdullah Gira

Posted on

Week 2: Natural Recursion

One drawback of this course is that it introduces a new language, so we spend most of the time learning the aspects of this language instead of learning new concepts. This week is no exception. It introduces arrays and how to define functions and data definitions for them in SDL.

Arrays

Arrays in SDL have no indexes, you only have two methods, first and rest. The first method returns the first element in the array, and the rest returns the array without the first element. Therefore we use natural recursion to loop over the array.

Natural Recursion

Natural recursion refers to the process in which a function calls itself in order to solve a problem.

Example code in python

# first -> returns the first element in the array
def first(arr):
    return arr[0]

# rest -> returns the rest of the array
def rest(arr):
    return arr[1:]

# return true if all array elements are true
def all_true(arr):
    if len(arr) == 0: return True
    return first(arr) and all_true(rest(arr))
Enter fullscreen mode Exit fullscreen mode

Importance of data design

The design of the data is actually making important decisions about the design of functions. (ref)

This post is part of a series about how I plan to revisit the CS bachelor and post a weekly update. You can read more about it here.

Top comments (0)