Código:
neg = Function[w,
If[w == {}, {},
If[First[w] < 0,
Prepend[neg[Rest[w]], First[w]],
neg[Rest[w]]
]]];
Execução:
In: neg[{-8, 2, 3, 4, -2, -8}]
Out: {-8, -2, -8}
Explicação:
Definir a função neg e variável a usar:
neg = Function[w,Se lista vazia, retorna lista vazia:
If[w == {}, {},Se não, se primeiro de w menor que zero(negativo):
If[First[w] < 0,Acrescentar no início da nova lista o primeiro de w e repetir o processo.
Prepend[neg[Rest[w]], First[w]],Se não, repetir o processo:
neg[Rest[w]]Fechar If's e Function:
]]];