11

In school exercice

I have this Function

bar :: Float -> Float -> Float
bar x 0 = 0
bar 0 y = 0
bar x y = x * y 

I type it in GHC as

let bar x 0 = 0; bar 0 y = 0; bar x y = x * y

and evaluate

bar foo 0
bar 0 foo

I'm asked to modify bar to use '|' so I want to do something like :

let bar x y = | x 0 = 0 | 0 y = 0 | x y = x * y

but in ghci I got

parse error on input '='

How can I do it in GHCi ? Will the fact of using pattern matching ('|') change something ?

IggY
  • 3,005
  • 4
  • 29
  • 54

3 Answers3

19

Look at the syntax for using guards:

bar x y | x == 0     = 0
        | y == 0     = 0
        | otherwise  = x * y

Written on one line in GHCi:

let bar x y | x == 0 = 0 | y == 0 = 0 | otherwise = x * y
md2perpe
  • 3,372
  • 2
  • 18
  • 22
14

Use files

Don't type your code directly into ghci unless it really is a one-liner.

Save your code in a text file called PatternMatch.hs and load it in ghci by typing.

:l PatternMatch.hs

and then if you make changes (and save) you can reload the file in ghci by typing

:r

Alternatively, you could name your files after which exercise they are in, or just have a reusablle Temp.hs if it really is throwaway code.

By saving stuff in a text file you make it much more easily editable and reusable.

Modules

Later you'll collect related functions together using a proper module, so they can be importer into other programs. For example, you could have

module UsefulStuff where

pamf = flip fmap

saved in a file called UsefulStuff.hs and then in another file you could

import UsefulStuff

and then use the functions from UsefulStuff there.

Modules are overkill for what you're doing now, but getting the workflow of edit, save, recompile, test, repeat, you'll save yourself from quite a bit of effort.

AndrewC
  • 32,300
  • 7
  • 79
  • 115
  • 1
    Welcome to the 21st century. Files belong in a cabinet with a brass handle. In this day and age where the cool kids plonk their code in JSFiddle and pounce on the Run button or dabble around in a Swift sandbox for instant gratification, this answer isn't going to win over many potential Haskell programmers. The really cool kids are probably heading over to [Shen](http://www.shenlanguage.org/) where they can have all the functional programming and type checking goodness they want, and then some. And oh yes, it's got a REPL which allows multi-line input "out of the box", type signatures and all. – ack Dec 10 '15 at 16:33
  • 7
    @ack I think you mistake me for a person who cares what cool kids do, whereas I actually only care about helping people to get the most out of the tools I also use. Thanks for the spam about sham. – AndrewC Dec 10 '15 at 16:40
  • 1
    If you care that much, maybe you could have referred to [this answer](http://stackoverflow.com/a/8443096/588561), which doesn't require the use of files. – ack Dec 10 '15 at 16:43
  • 6
    @ack Honestly, I think the cool kids are going to struggle to build anything non-trivial unless they're prepared to save some code in some files. If you advocate your favourite language by saying you don't need to save any of your work, I'm afraid you'll end up selling it as a toy language not suitable for serious development. Anyway, I think this whole comment conversation is off topic. – AndrewC Dec 10 '15 at 16:58
1

GHCi allows multi-line input by entering :set +m in the interpreter. See Multiline input section for more details.

Here's an example that demonstrates it:

GHCi, version 8.8.3: https://www.haskell.org/ghc/  :? for help
Prelude> :set +m
Prelude> { incList [] = []
Prelude| ; incList (x:xs) = x+1:incList xs
Prelude| }
Prelude> incList [40, 41, 42]
[41,42,43]
Prelude>
user3496912
  • 195
  • 1
  • 6