jaberwacky

Haskell

12 posts in this topic

#1 ·  Posted (edited)

This thread is intended to document my progress and small epiphanies as I learn this strange and wonderful language.

So I'm following this tutorial: https://en.wikibooks.org/wiki/Yet_Another_Haskell_Tutorial which has been awesome so far

I came across the following exercise:

Define a recursive function mult that takes two positive integers a and b and returns a*b, but only uses addition (i.e., no fair just using multiplication). Begin by making a mathematical definition in the style of the previous exercise and the rest of this section.

And I solved it!  I know.  I know.  Big whoop.  It's great for me as someone who started out as a non-math type.  The solution I came up with isn't exactly like the official solution though, but it seems to work.

rec a 0 = 0
rec a b = 
    a + (rec a (b - 1))

 

Edited by jaberwacky

Share this post


Link to post
Share on other sites



Honestly being such a functional programming newb, I don't know exactly.  I guess mostly because there are a lot of tutorials and other resources based on Haskell.  I also see the syntax as being the most simple at this point.  I could very well change from Haskell to something else as I learn.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

FINALLY! After much hair pulling, forehead smashing, and crying I finally hacked up this haskell program that takes a sentence that the user enters and then reverses the inner letters of each word.  For instance this sentence, "Why did you pick Haskell as the next language to learn" becomes this, "Why did you pcik Hleksal as the nxet lgaugnae to lraen".

I'm so relieved.  I can sleep now.

module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering

    main_rec
        
main_rec = do
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    if toLower sentence == "exit" then do
        print "Bye!"
    else do     
        let word_list = words sentence
        
        let scrambled = map thaScrambla word_list
    
        print scrambled 
        
        main_rec
    
-- Designed to scramble your internals.
thaScrambla :: [Char] -> [Char] 
thaScrambla word 
    | length word > 3 = [head word] ++ (reverse $ tail $ init word) ++ [last word]
    | otherwise       = word

toLower :: [Char] -> [Char] 
toLower word 
    | word == "Exit" = "exit"
    | otherwise      = word
module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering

    main_rec
        
main_rec = do
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    if toLower sentence == "exit" then do
        print "Bye!"
    else do     
        let word_list = words sentence
        
        let scrambled = map thaScrambla word_list
    
        print scrambled 
        
        main_rec
    

thaScrambla :: [Char] -> [Char] 
thaScrambla word = do
    if length word > 3 then do
        [head word] ++ (reverse $ tail $ init word) ++ [last word]
    else word

toLower :: [Char] -> [Char] 
toLower "Exit" = "exit"
toLower x = x
module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering
    
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    let word_list = words sentence
    
    let scrambled = map thaScrambla word_list
    
    print scrambled
    
thaScrambla word = do
    if length word > 3 then do
        [head word] ++ reverse (tail (init word)) ++ [last word]
    else word

Bizarro World: FLLANIY Aetfr mcuh hiar pnillug faeherod snihsamg and cniyrg I fllaniy hekcad up tihs hleksal pargorm taht tekas a scnetnee taht the uesr eretns and tehn resreves the iennr lrettes of ecah wrod   

Edited by jaberwacky
better code

Share this post


Link to post
Share on other sites

I've always wanted to learn a pure functional language (seewhatididthere). Right now I just sprinkle the functional stuff on my imperative programs where it's really neat for solving a particular problem.  How's Haskell for you so far (apart from the hair pulling)?

Share this post


Link to post
Share on other sites

Bizarro World: FLLANIY Aetfr mcuh hiar pnillug faeherod snihsamg and cniyrg I fllaniy hekcad up tihs hleksal pargorm taht tekas a scnetnee taht the uesr eretns and tehn resreves the iennr lrettes of ecah wrod   

Translato: Bizarre World: FINALLY After much hair pulling forehead smashing and crying I finally ?????? up this haskell program that takes a sentence that the user enters and then reverses the inner letters of each word

 

I used a proprietary language which is used in a proprietary system to translate it back into English... That system is homo sapian and the lagauage is homo sapianian I think... TD :P


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

How's Haskell for you so far (apart from the hair pulling)?

I wouldn't take my opinion as fact.  I approach Haskell and functional programming from a place of little to no mathematical ability.  It's been a hair pulling experience.  Since my last post I haven't done much Haskell particular stuff.  Just reading about general functional programming concepts. 

In my experience, I have been slowly arriving to a functional style in my imperative scripts.  So I have been picking up on the concepts pretty well as long as they're explained from a practical view point.  I don't do very well with the category theory stuff.  Supposedly that stuff isn't necessary to program functionally though.

Edited by jaberwacky

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

It just occurred to me that Haskell is the first language that I've tried to learn where I did not give one thought to syntax highlighting.

Edit:

Actually, that's not entirely true.  I did try to set up SciTE to use the Haskell syntax highlighter, but the option isn't there in the language drop down.  Still, the point stands, it's not that big of a deal for some reason.  Just a thought.

Edited by jaberwacky

Share this post


Link to post
Share on other sites

Even Notepad++ supports Haskell highlighting, though I'm not sure how good it is.

I've been thinking about learning F# for a while.

1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Wow! F# is multi-platform, supports android & no spaghetti code! Just what I need! Thanks Manadar & Jaber for the link, TD :thumbsup:

Edited by TheDcoder
blank line at the start

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • jaberwacky
      By jaberwacky
      Hi!  I've been diving into functional programming lately.  Really neat stuff!  So here is my first Haskell program (worthy of being called a program?)
      fizz :: Int -> Bool fizz x = if x `mod` 3 == 0 then True else False buzz :: Int -> Bool buzz x = if x `mod` 5 == 0 then True else False fizzbuzz = [if x `mod` 15 == 0 then               "FizzBuzz"             else if fizz x then               "Fizz"             else if buzz x then               "Buzz"             else               show x | x <- [1..100]] This program is the FizzBuzz program described here: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
      Second version with the guidance of a post from that page I linked:
      fizz :: Int -> Bool fizz x = if mod x 3 == 0 then True else False buzz :: Int -> Bool buzz x = if mod x 5 == 0 then True else False fizzbuzz :: Int -> [Char] fizzbuzz x | mod x 15 == 0 = "FizzBuzz" | fizz x = "Fizz" | buzz x = "Buzz" fizzbuzz x = show x To run: map fizzbuzz [1 .. 100]