Сейчас потянуло на функциональное программирование. Выбрал Haskell как наиболее универсальный ЯП.
λ> myfun = (^2)
λ> myfun <$> [1, 2, 3]
[1,4,9]
λ> myfun = (^2)
λ> map myfun [1, 2, 3]
[1,4,9]
Есть функция:
pow2 x = x ^ 2
Есть такой список:
[1, 2, 3, 4, 5]
Как вызвать функцию для всех элементов списка
squares :: Num a => [a] -> [a]
squares lst = do
x <- lst
return (x ^ 2)
squares' :: Num a => [a] -> [a]
squares' lst = lst >>= \x -> return (x ^ 2)
squares'' :: Num a => [a] -> [a]
squares'' lst = [x ^ 2 | x <- lst]
fx f lst = [f x | x <- lst]
main = do
print $ fx (^2) [1, 2, 3]
print $ squares [1, 2, 3]
print $ squares' [1, 2, 3]
print $ squares'' [1, 2, 3]
module Main where
import System.IO
main :: IO ()
main = do
content <- readFile "file.dat"
putStrLn content
putStr "enter x = "
-- Флюшим вывод не забываем про ленивость языка
hFlush stdout
x <- getLine
print (read x :: Int)
writeFile "file.dat" x
Надо вот это --> For a comprehensive tutorial on using IO monad, look at the Haskell I/O inside: Down the Rabbit's Hole
main = do
putStrLn "Start point: "
start <- getArg
putStrLn "End point: "
end <- getArg
putStrLn "Max cost: "
costStr <- getArgNum
let cost = read costStr :: Double
mapM_ putStr $ astar graph1 start end cost
putStrLn " "
----------------CHECK BUKVI--------
getArg :: IO String -- И так все что ниже
getArg = do
line <- getLine
getArgNum :: IO Double
getArgNum = do
line <- getLine
if numOk line
then return (read line :: Double)
else do
putStrLn "Please input a proper number:"
getArgNum -- У тебя было getArg
costStr <- getArg -- у тебя было getArgNum
let cost = read costStr :: Double
много читал по этой теме
isSorted :: (Ord a) => (a -> a -> Bool) -> [a] -> Bool
isSorted cmp xs = and (zipWith cmp xs (tail xs))
cp :: Int -> Int -> Bool
cp x y = x <= y
main = print (isSorted cp [1,2,7,4,5]) -- False
main = print (isSorted cp [1,2,3,4,5]) -- True