Call us +96895672917 | +917736882141

Project Description

Translate the Prolog Task, into a Haskell Task

% Muhsen “badeed” Saleh
% Homework 3
% Made with help from https://www.tutorialspoint.com/prolog/index.htm and CS320 discord
rhs(NUM1, NUM2, I) :-
I_ is I – 1, I > 1, between(1, I_ ,A), between(1, I_, B), expression(NUM1, A), expression(NUM2, B), I is A + B.

 

expression(10, 1).
expression(NUM1 + NUM2, I) :- rhs(NUM1, NUM2 , I).
expression(NUM1 – NUM2, I) :- rhs(NUM1, NUM2 , I).
expression(NUM1 * NUM2, I) :- rhs(NUM1, NUM2 , I).
expression(NUM1 / NUM2, I) :- rhs(NUM1, NUM2 , I), BOT is NUM2, BOT > 0, TOP is NUM1, 0 is mod(TOP,BOT).

important_nums(I) :- expression(NUM1, I), RES is NUM1, between(0, 9, RES), write(NUM1), write(‘ = ‘), write(RES), nl.

main :-
write(‘Expressions of interest2: ‘), nl, aggregate_all(count, important_nums(4), Count), nl, write(‘Total expressions ofinterest: ‘), write(Count), nl.

Haskell coding which is converted

 

import Data.List

data Expr a = Plus (Expr a) (Expr a) | Minus (Expr a) (Expr a) | Divide (Expr a) (Expr a) | Multiply (Expr a) (Expr a) | Value Integer | NullExpr

— add’ :: Expr a -> Expr a
add a b = Plus a b

— subtract’ :: Expr a -> Expr a
subtract’ a b = Minus a b

— div’ :: Expr a -> Expr a
div’ a b = Divide a b

— mul’ :: Expr a -> Expr a
mul’ a b = Multiply a b

evaluate :: Expr a -> Integer
evaluate NullExpr = -0xdeadbeef
evaluate (Value a) = a
evaluate (Plus a b) = (evaluate a) + (evaluate b)
evaluate (Minus a b) = (evaluate a) – (evaluate b)
evaluate (Divide a b)
| y == 0 = evaluate NullExpr
| x `mod` y == 0 = x `div` y
| otherwise = evaluate NullExpr
where x = evaluate a
y = evaluate b
evaluate (Multiply a b) = (evaluate a) * (evaluate b)

instance Show (Expr a) where
show (NullExpr) = “”
show (Plus a b) = (show a) ++ “+” ++ (show b)
show (Minus a b) = (show a) ++ “-” ++ (show b)

show (Divide (Value a) (Value b)) = (show a) ++ “/” ++ (show b)
show (Divide (Value a) b) = (show a) ++ “/(” ++ (show b) ++ “)”
show (Divide a (Value b)) = “(” ++ (show a) ++ “)/” ++ (show b)
show (Divide a b) = “(” ++ (show a) ++ “)/(” ++ (show b) ++ “)”

show (Multiply (Value a) (Value b)) = (show a) ++ “*” ++ (show b)
show (Multiply (Value a) b) = (show a) ++ “/(” ++ (show b) ++ “)”
show (Multiply a (Value b)) = “(” ++ (show a) ++ “)/” ++ (show b)
show (Multiply a b) = “(” ++ (show a) ++ “)*(” ++ (show b) ++ “)”

show (Value a) = (show a)

length’ :: Expr a -> Integer
length’ (Value a) = 1
length’ (Plus a b) = (length’ a) + (length’ b)
length’ (Minus a b) = (length’ a) + (length’ b)
length’ (Divide a b) = (length’ a) + (length’ b)
length’ (Multiply a b) = (length’ a) + (length’ b)

valsofinterest :: (Eq t, Num t) => t -> [Expr a]
valsofinterest 1 = [Value 10]
valsofinterest n = (map (\(x, y) -> Plus x y) prod) ++
(map (\(x, y) -> Minus x y) prod) ++
(map (\(x, y) -> Divide x y) prod) ++
(map (\(x, y) -> Multiply x y) prod) ++ xs
where prod = [(x, y) | x <- xs, y <- xs]
xs = valsofinterest (n – 1)

showexprsofinterest :: [String]
showexprsofinterest = nub $ map
(\x -> (show x) ++ ” = ” ++ (show (evaluate x))) $
filter (\x -> (evaluate x) < 10 &&
(evaluate x) >= 0 &&
(length’ x) == 4)
(valsofinterest 4)

Project Details

  • Date November 24, 2021
  • Tags Functional programming, Haskell Programming, Programming
Trustpilot