haskell - partial applied function in the recursion -


i beginner in haskell. wrote function, applies function several times argument:

frepeat :: (integral n) => n -> (a -> a) -> -> frepeat n f     | n <= 0 = error "invalid count."     | n == 1 = f     | otherwise = f (frepeat (n-1) f a) 

it works:

ghci> frepeat 3 (^2) 2 256 ghci> frepeat 4 (++ "-bla") "bla" "bla-bla-bla-bla-bla" 

now want rewrite more compactly, without last argument. want - must partial applied function. tried this:

frepeat :: (integral n) => n -> (a -> a) -> -> frepeat n f     | n <= 0 = error "invalid count."     | n == 1 = f     | otherwise = f (frepeat (n-1) f) 

but ghci doesn't eat it... mean can't it?

you need 1 (.) in last part

| otherwise = f . (frepeat (n-1) f) 

in general, this

let f x = f (g x) 

can rewritten this

let f = f . g 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -