Вопрос

I have the following program:

mnr = [0,1,2,3,4,5,6]  :: [Int]
name = "Max Mustermann" :: String
kzn = "e53X" :: String

t1 = ("p1",(take 2.tail)mnr, (take 3.words.(let no n= name;in no))"No");
{-result: t1 == ("p1",[1,2],["Max","Mustermann"]) -}

Why is "No" ignored and what does let no n= name;in no do?

Это было полезно?

Решение

This is pretty crazily written code. Let's try to resolve it piece by piece.

First of all, t1 is a tuple of type (a,b,c). Let's write t1 = (x,y,z) and try to figure things out. It's pretty clear that a is String, and x is "p1".

The second component is ((take 2) . tail) mnr, which is take 2 [1,2,3,4,5,6], which is [1,2]. So b is [Int], and y is [1,2]. Makes sense.

The third component is oddly written. First of all,

let no n = name in no

simply defines a function \n -> name, i.e. the constant function returning name. This is much more nicely written as const name. So you have the composition (take 3) . words . (const name), and you apply it to "No". Well, (const name) "No" is obviously just name, i.e. "Max Mustermann". So words applied to this is ["Max", "Mustermann"], and then take 3 gets you the first three elements, which is again ["Max", "Mustermann"]. That's z above, and shows that c is [String].

All in all, you end up with t1 having type (String, [Int], [String]), and value ("p1", [1,2], ["Max", "Mustermann"]).


The code for the third component should be cleaned up, but how you clean it up depends on what you really want to do onwards. As it stands now, however, it's very convoluted and will just confuse you and others.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top