Another rescue for JavaScript
double a = a * 2
double :: Int -> Int
double a = a * 2
add :: Int -> Int -> Int
add a b = a + b
sumToTen :: Int -> Int
sumToTen n =
if n == 10 then
10
else
sumToTen (n + 1)
main = log (show (sumToTen 0))
"10"
factors :: Int -> Array (Array Int)
factors n = filter (\xs -> product xs == n) do
i <- 1 .. n
j <- i .. n
[ [ i, j ] ]
logSomething :: String -> Effect Unit
logSomething msg = log ("Message: " <> msg)
main = logSomething "Hello"
import Effect (Effect)
main = log "Hello world"
main = do
log "Hello world"
log "Hello World"
name = "John Doe"
main = log (show name)
double :: Int -> Int
double x = x * 2
main = log (show (double 2))
doubleNumber :: Int -> Int
doubleNumber x = x * 2
printDoubleNumber :: Int -> String
printDoubleNumber x = "The Result: " <> (show (doubleNumber x))
main = log (printDoubleNumber 2)
"The Result: 4"
type Person =
{ name :: String,
age :: Int }
max :: Person
max = { name: "Max", age: 22 }
main = log (show max)
-- { age: 22, name: "Max" }
main = log (show max.name)
-- "Max"
-- using a function to access it:
getName :: Person -> String
getName person = person.name
main = log (getName max)
biggerThan10 :: Int -> String
biggerThan10 num =
if num > 10
then "Number is > 10"
else "Number is NOT > 10"
main = log (biggerThan10(2))
"Number is NOT > 10"
test :: Boolean -> String
test condition =
if condition
then "true"
else "false"
main = log (test(1 > 2))
"false"
map (\n -> n * 2) [1, 2, 3]
[2, 4, 6]
addOne :: Int -> Int
addOne x = x + 1
newArr = map addOne [1, 2, 3]
-- for a predefined function
map show [1, 2, 3]
-- ["1","2","3"]
filter (\n -> mod n 2 == 0) [1, 2, 3, 4, 5, 6]
[2, 4, 6]
range 0 5
-- [0,1,2,3,4,5]
-- or:
(0 .. 5)
concat [[1, 2, 3], [4, 5]]
-- [1,2,3,4,5]
map (\n -> [n, n * n]) (1 .. 5)
-- [[1,1],[2,4],[3,9],[4,16],[5,25]]
-- A two-dimensional array? Let's fix this:
concatMap (\n -> [n, n * n]) (1 .. 5)
-- [1,1,2,4,3,9,4,16,5,25]
-- Much better!
import Data.Foldable
foldl (+) 0 [1, 2, 3]
-- 6
foldr (+) 0 [1, 2, 3]
-- 6
foldl (*) 0 [1, 2, 3, 4]
-- 0
foldl (*) 1 [1, 2, 3, 4]
-- 24
multiply :: Int -> Int -> Int
multiply a 0 = 0
multiply 0 a = 0
multiply a b = a + b
toString :: Boolean -> String
toString true = "true"
toString false = "false"
isEmpty :: forall a. Array a -> Boolean
isEmpty [] = true
isEmpty _ = false
biggerNumber :: Int -> Int -> Int
biggerNumber a b | a > b = a
| otherwise = b
enterNumber:: Int -> String
enterNumber num = case num of
0 -> "You entered 0"
1 -> "You entered 1"
2 -> "You entered 2"
_ -> "You entered a different number"