Jump to content

Substring/Top and tail: Difference between revisions

→‎{{header|Python}}: (added other two example strings)
m (Reverted an inadvertent edit)
(→‎{{header|Python}}: (added other two example strings))
Line 1,350:
 
def main():
printfor xs in transpose(
ap([tail, init, composechunksOf(init)(tail)]3)(
ap(["knights"tail, init, compose(init)(tail)])(
['knights', 'socks', 'brooms']
)
)
))
 
):
# --> ['nights', 'knight', 'night']
print(xs)
 
 
Line 1,378 ⟶ 1,379:
), fs, []
)
 
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
return lambda xs: reduce(
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []
) if 0 < n else []
 
 
Line 1,383 ⟶ 1,392:
def compose(g):
return lambda f: lambda x: g(f(x))
 
 
# transpose :: [[a]] -> [[a]]
def transpose(xs):
return list(map(list, zip(*xs)))
 
 
Line 1,388 ⟶ 1,402:
main()</lang>
{{Out}}
<pre>['nights', 'knight', 'night']</pre>
['ocks', 'sock', 'ock']
['rooms', 'broom', 'room']</pre>
 
=={{header|Racket}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.