Questions tagged [partial-functions]

Partial functions are functions which are not defined for all of their inputs. For questions about partially applied functions, use [partial-application] instead.

35 questions
72
votes
3 answers

What exactly is meant by "partial function" in functional programming?

According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python: >>> def add(x,y): ... return x+y ... >>> new_function =…
34
votes
2 answers

In Haskell, why non-exhaustive patterns are not compile-time errors?

This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function? I understand that using -Wall, GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not…
10
votes
2 answers

Why is take a total function

take (-1) [] is []. What are the reasons to prefer this over a partial function, that is, an error? Are there use cases where this property is exploited?
false
  • 10,264
  • 13
  • 101
  • 209
9
votes
5 answers

Scala total function as partial function

Since a total function is a special case of a partial function, I think I should be able to return a function when I need a partial. Eg, def partial : PartialFunction[Any,Any] = any => any Of course this syntax fails to compile. My question is, is…
sksamuel
  • 16,154
  • 8
  • 60
  • 108
6
votes
3 answers

Can any partial function be converted to a total version in Haskell?

So far I have seen numerous "Maybe" versions of certain partial functions that would potentially result in ⊥, like readMaybe for read and listToMaybe for head; sometimes I wonder if we can generalise the idea and work out such a function safe :: (a…
Futarimiti
  • 551
  • 2
  • 18
6
votes
3 answers

Scala's Partial Functions in Haskell

Scala has a very nice support of partial functions, mainly because in Scala when you define a partial function it also defines an isDefinedAt function for it. And also Scala has orElse and andThen functions to work with partial functions. Haskell…
Gurmeet Singh
  • 395
  • 1
  • 6
6
votes
1 answer

How does Scala Cons pattern matching determine the head and the tail of a List?

How is the head and tail determined in the following statement: val head::tail = List(1,2,3,4); //head: 1 tail: List(2,3,4) Shouldn't there be some piece of code which extracts the first element as head and returns the tail as a new List. I've…
6
votes
5 answers

Short circuiting a list mapping with a partial function

So, I have made this function called tryMap which is as follows: /// tryMap, with failure and success continuations. let rec tryMapC : 'R -> ('U list -> 'R) -> ('T -> 'U option) -> ('T list) -> 'R = fun failure success mapping list -> …
phaz
  • 872
  • 9
  • 23
5
votes
1 answer

Is scanl1 really partial?

According to the Haskell wiki, the scanl1 function is partial. I don't understand what inputs result in bottom, though. For list functions, I'm used to the problem inputs either being empty lists (like for head) or infinite ones (like for reverse).…
4
votes
1 answer

How to getOrElse with another Option in Scala

Let's assume that we have an option foo1 and an option foo2: val foo1: Option[Foo] val foo2: Option[Foo] Is there an operator/function that allows me to return the value of the foo2 when foo1 is None? val finalFoo: Option[Foo] =…
Yuchen
  • 30,852
  • 26
  • 164
  • 234
4
votes
3 answers

Anonymous PartialFunction syntax

I asked this question earlier: Combine a PartialFunction with a regular function and then realized, that I haven't actually asked it right. So, here goes another attempt. If I do this: val foo = PartialFunction[Int, String] { case 1 => "foo" } …
Dima
  • 39,570
  • 6
  • 44
  • 70
2
votes
2 answers

Partial function explanation in the Odersky book

In the Scala Odersky book, he has an example explaining partial functions of page 295. It starts with this function: val second: List[Int] => Int = { case x :: y :: _ => y } So the above function will succeed if you pass it a three element list…
Jwan622
  • 11,015
  • 21
  • 88
  • 181
2
votes
0 answers

Anonymous partial function in early initializer requires "premature access to class"

Why does this fail to compile: trait Item trait StringItem extends Item { def makeString: String } trait SomeOtherItem extends Item trait DummyTrait case class Marquee(items: Seq[Item]) extends { val strings: Seq[String] = items.collect { …
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
2
votes
1 answer

Applying partial functions where defined and a different function where not

This is a motivational example, Given: List((1,2), (2,1), (3,1)) I'd like to return: List((1,2),(3,1)) I've tried to do this in several ways. First: List((1,2), (2,1), (3,1)) map { case (a,b) => if (a > b) (a,b) else (b,a) } distinct Then I…
Joselo
  • 93
  • 1
  • 8
2
votes
1 answer

Explanation of List[_] in pattern matching used in flatten function

I am new to scala and I can not understand the following function val L = List(List(1, 1), 2, List(3, List(5, 8))) def flatten(l: List[Any]): List[Any] = l flatMap { case ms:List[_] => flatten(ms) case l => List(l) } …
Donbeo
  • 17,067
  • 37
  • 114
  • 188
1
2 3