Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

I got this question at a test:

Write a function f with the following type a -> b -> (a -> b). a and b should not be bound in any sense, the shorter the code, the better.

I came up with this: f a b = \x -> snd ([a,x],b)

Can you find something tinier ?

Currently the winner is: f _=(.f).const

share|improve this question
    
If a more general type is allowed: f = const const. –  hammar Feb 13 at 12:22
    
@hammar: or f _ b _ = b, but, given the solution in the question, I suspect a more general type is not allowed. –  Tikhon Jelvis Feb 13 at 12:31
2  
If a more general type is allowed, why not f = id? –  Tom Ellis Feb 13 at 13:18
4  
In fact if a more general type is allowed then f = f is a solution, so I guess the conditions on the type are very important! –  Tom Ellis Feb 13 at 13:56
1  
A more general type is not allowed, your assumptions were correct. –  Radu Stoenescu Feb 14 at 9:28
show 1 more comment

2 Answers 2

Your example can be shrunk by getting rid of the anonymous function on the right-hand side:

f a b x = snd ([a,x],b)

This works because the type a -> b -> a -> b is equivalent to a -> b -> (a -> b) in Haskell.

share|improve this answer
2  
Slightly shorter modification: f a b x = snd (f x,b) –  Ed'ka Feb 14 at 4:40
add comment

Given ScopedTypeVariables, I came up with this:

f (_::a) b (_::a) = b

If you shrink down both my function and yours, mine is a hair shorter:

f(_::a)b(_::a)=b
f a b x=snd([a,x],b)

Of course, you're probably not allowed to rely on ScopedTypeVariables :P.

share|improve this answer
2  
This is not as short as f _=(.f).const (due to Sassa NF). Which also doesn't need ScopedTypeVariables. –  leftaroundabout Feb 13 at 12:07
    
Hmm, I initially thought this would require the first and third arguments to be lists... –  Chris Taylor Feb 13 at 12:29
    
@ChrisTaylor: Too much OCaml on the mind? :) –  Tikhon Jelvis Feb 13 at 12:30
    
Hah, must be! ;) –  Chris Taylor Feb 13 at 12:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.