Higher-order user-defined functions in haskell

I went through several Haskell training examples, but I couldn’t figure out how to write higher-order custom functions in Haskell

if we take a parameter as a function, how is the type of function identifier determined?

+3
source share
2 answers

Use the function mapas a simple example. mapaccepts a function and a list and applies this function to all elements of the list. If you write a signature map, it is done as follows:

. , a -> b. . , [a]. : a -> b a? , b. , [b]. :

map :: (a -> b) -> [a] -> [b]

:

map f []     = []
map f (x:xs) = f x : map f xs

?

+10

. , . , [grin].

, twice compose. . .

$ ghci
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let twice f x = f (f x)
Prelude> :t twice
twice :: (t -> t) -> t -> t
Prelude> let compose f g x = f (g x)
Prelude> :t compose
compose :: (t1 -> t2) -> (t -> t1) -> t -> t2
Prelude> 

, twice (t->t) a t resulkt t. t - ( 4 ).

.

Prelude> let times3 x = x * 3
Prelude> let times5 x = x * 5
Prelude> twice times3 2
18
Prelude> twice times5 2
50
Prelude> compose times3 times5 2
30
Prelude> 

Ans :

Prelude> (twice twice) times3 2
162
Prelude> twice (twice times3) 2
162
Prelude> 

, ?

+7

All Articles