Racket – liste, iterazioni e ricorsività – 1

super_chickenOggi sono qui: [doc]/guide/Lists__Iteration__and_Recursion.html. Racket is a dialect of the language Lisp, whose name originally stood for “LISt Processor.” The built-in list datatype remains a prominent feature of the language. The list function takes any number of values and returns a list containing the values: l0 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, [probabilmente Python] and other languages. The body of a Racket iteration is packaged into a function to be applied to each element, so the lambda form becomes particularly handy in combination with iteration functions. Different list-iteration functions combine iteration results in different ways. The map function uses the per-element results to create a new list: l1 The andmap and ormap functions combine the results by anding or oring: l2 The filter function keeps elements for which the body result is true, and discards elements for which it is #f: l3 Fin qua tutto tranquillo ma pronti per il passo successivo… The map, andmap, ormap, and filter functions can all handle multiple lists, instead of just a single list. The lists must all have the same length, and the given function must accept one argument for each list: l4 The foldl function generalizes some iteration functions. It uses the per-element function to both process an element and combine it with the “current” value, so the per-element function takes an extra first argument. Also, a starting “current” value must be provided before the lists: l5 Uh! però è risaputo: Despite its generality, foldl is not as popular as the other functions. One reason is that map, ormap, andmap, and filter cover the most common kinds of list loops. Racket provides a general list comprehension form for/list, which builds a list by iterating through sequences. Ma di questo se ne parlerà un’altra volta.

Iterazioni base di liste

Difficile tradurre “from scratch”, “da zero”? o come? 😳 Although map and other iteration functions are predefined, they are not primitive in any interesting sense. You can write equivalent iterations using a handful of list primitives. Since a Racket list is a linked list, the two core operations on a non-empty list are:

  • first: get the first thing in the list;
  • rest: get the rest of the list.

l6 To create a new node for a linked list—that is, to add to the front of the list—use the cons function, which is short for “construct.” To get an empty list to start with, use the empty constant: l7 Ovviamente c’è la funzione list se si sa a priori cosa metterci dentro: l8 To process a list, you need to be able to distinguish empty lists from non-empty lists, because first and rest work only on non-empty lists. The empty? function detects empty lists, and cons? detects non-empty lists: l9 Con queste possiamo definire la nostra versione delle funzioni length e map: l10 Poi una frase di quelle che mi piacciono: If the derivation of the above definitions is mysterious to you, consider reading How to Design Programs. If you are merely suspicious of the use of recursive calls instead of a looping construct, then read on. Sì la documentazione di Racket è eccezionale 😀 Pausa :mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: