Pregunta

The language used here is FP.

I don't understand the difference between the use of <> and [].

For example:

2 : <3,4,5> -> 4

But

+ o [1,2] : <2,3> -> 5

Why is the first sequence written with [] instead of <> ?

Thanks!

¿Fue útil?

Solución

The answer to your question is actually in the wiki link you provided. While <...> is used as notation for lists (e.g., <3,4,5> is a list with elements 3, 4, and 5), [...] is just syntax for a functional that is already provided by FP. It is called construction and defined by

[f1, ..., fn]:x =  <f1:x, ..., fn:x>

If you are familiar with Haskell: it is similar to

map (\f -> f x) [f1, ..., fn] = [f1 x, ..., fn x]

that is to say, that [...] is some kind of map function, see also Higher order function to apply many functions to one argument.

In your specific examples

2:<3, 4, 5> 

selects the second element of the given list, and + o [1, 2]:<2, 3> can be "evaluated" as follows (where o is function composition):

+ o [1, 2]:<2, 3>  => (definition of composition)
+:([1, 2]:<2, 3>) => (definition of construction)
+:<1:<2, 3>, 2:<2, 3>> => (select list elements)
+:<2, 3> => (addition)
5
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top