Buscar

3. (4 points) Using recursion and pattern matching, write in SML a function f: int list -> bool that receives a list l of integers and returns true...

3. (4 points) Using recursion and pattern matching, write in SML a function f: int list -> bool that receives a list l of integers and returns true if and only if one of the elements of l is even.


Essa pergunta também está no material:

Prova DCC024 Linguagens de Programação
3 pág.

Conceitos de Linguagens de Programação Universidade Federal de Minas GeraisUniversidade Federal de Minas Gerais

💡 1 Resposta

User badge image

Ed Verified user icon

Aqui está uma possível solução em SML para a função f: ``` fun f [] = false | f (x::xs) = if x mod 2 = 0 then true else f xs ``` Essa função usa pattern matching para tratar o caso base (quando a lista é vazia) e o caso recursivo (quando a lista tem pelo menos um elemento). Se o primeiro elemento da lista for par, a função retorna true. Caso contrário, a função é chamada recursivamente com o restante da lista. Se a lista inteira for percorrida e nenhum elemento for par, a função retorna false.

0
Dislike0

✏️ Responder

SetasNegritoItálicoSublinhadoTachadoCitaçãoCódigoLista numeradaLista com marcadoresSubscritoSobrescritoDiminuir recuoAumentar recuoCor da fonteCor de fundoAlinhamentoLimparInserir linkImagemFórmula

Para escrever sua resposta aqui, entre ou crie uma conta

User badge image

Outros materiais