set i read set f "" print map index i k v return label k set x _ set _ slice " " length index f e 1 1 set f concat f wrap x return label e set _ slice " " add _ multiply -1 x 1 1 return label v set _ unwrap slice i _ add 1 _ 1 return
这是一个完整的程序,从STDIN输入,在STDOUT输出。
包装版本,具有美学价值:
set i read set f "" print map index i k v return label k set x _ set _ slice "
" length index f e 1 1 set f concat f wrap x return label e set _ slice " " add
_ multiply -1 x 1 1 return label v set _ unwrap slice i _ add 1 _ 1 return
还有一个高度“注释”的版本(通心粉中没有注释,因此我只使用裸字符串文字):
set input read "read line from STDIN, store in 'input' var"
set found "" "we need this for 'keep' below"
print map index input keep val "find indeces to 'keep', map to values, print"
return
label keep
"we're trying to determine which indeces in the string to keep. the special
'_' variable is the current element in question, and it's also the value
to be 'returned' (if the '_' variable is '0' or empty array after this
label returns, the index of the element is *not* included in the output
array; otherwise, it is"
set x _ set _ slice
" "
length index found exists
1
1
"now we're using 'index' again to determine whether our '_' value exists in
the 'found' array, which is the list of letters already found. then we
have to apply a boolean NOT, because we only want to keep values that do
NOT exist in the 'found' array. we can 'invert' a boolean stored as an
integer number 'b' (hence, 'length') with 'slice(' ', b, 1, 1)'--this is
equivalent to ' '[0:1], i.e. a single-character string which is truthy, if
'b' was falsy; otherwise, it results in an empty string if 'b' was truthy,
which is falsy"
set found concat found wrap x "add the letter to the 'found' array"
return
label exists
set _ slice
" "
add _ multiply -1 x
1
1
"commentary on how this works: since 0 is falsy and every other number is
truthy, we can simply subtract two values to determine whether they are
*un*equal. then we apply a boolean NOT with the method described above"
return
label val
set _ unwrap slice input _ add 1 _ 1 "basically 'input[_]'"
return
(这是第一个真正的通心粉程序(实际上可以执行某些操作)!\ o /)