Home > Article > Backend Development > What is the difference between PHP functions and Haskell functions?
The difference between PHP and Haskell functions is: Function signature: Optional in PHP, mandatory in Haskell. Parameter types: PHP loose, Haskell strict. Return value type: Optional in PHP, mandatory in Haskell.
The difference between PHP functions and Haskell functions
PHP and Haskell are both popular programming languages, but they differ in function definitions and There are big differences in how they are used.
Function signature
Example:
// PHP function add($a, $b) { return $a + $b; }
-- Haskell add :: Int -> Int -> Int add a b = a + b
Parameter type
Example:
// PHP add("1", 2); // 有效,但结果为 "12"
-- Haskell add "1" 2 -- 类型错误:参数类型不匹配
Return value type
// PHP
function print_hello() {
echo "Hello, World!";
}
-- Haskell
print_hello :: IO ()
print_hello = putStrLn "Hello, World!"
Consider a function that calculates the sum of elements in a list.
// PHP function sum_list($list) { $sum = 0; foreach ($list as $item) { $sum += $item; } return $sum; }rrree
The above is the detailed content of What is the difference between PHP functions and Haskell functions?. For more information, please follow other related articles on the PHP Chinese website!