Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:45+00:00 2026-06-17T08:58:45+00:00

I am learning Jason Hickey’s Introduction to Objective Caml . There is an exercise

  • 0

I am learning Jason Hickey’s Introduction to Objective Caml.

There is an exercise like this:

Exercise 4.3 Suppose we have a crypto-system based on the following substitution cipher, where each plain letter is encrypted according to the following table.

Plain     | A B C D
--------------------
Encrypted | C A D B

For example, the string BAD would be encrypted as ACB.

Write a function check that, given a plaintext string s1 and a ciphertext string s2, returns true if, and only if, s2 is the ciphertext for s1. Your function should raise an exception if s1 is not a plaintext string. You may wish to refer to the string operations on page 8. How does your code scale as the alphabet gets larger? [emphasis added]


Basically, I wrote two functions with might-be-stupid-naive ways for this exercise.

I would like to ask for advice on my solutions first.

Then I would like to ask for hints for the scaled solution as highlighted in the exercise.


Using if else

let check_cipher_1 s1 s2 = 
    let len1 = String.length s1 in
        let len2 = String.length s2 in              
            if len1 = len2 then
                    let rec check pos =
                        if pos = -1 then
                            true
                        else
                            let sub1 = s1.[pos] in
                                let sub2 = s2.[pos] in
                                    match sub1 with
                                        | 'A' -> (match sub2 with
                                                    |'C' -> check (pos-1)
                                                    | _ -> false)
                                        | 'B' -> (match sub2 with
                                                    |'A' -> check (pos-1)
                                                    | _ -> false)
                                        | 'C' -> (match sub2 with
                                                    |'D' -> check (pos-1)
                                                    | _ -> false)
                                        | 'D' -> (match sub2 with
                                                    |'B' -> check (pos-1)
                                                    | _ -> false)
                                        | _ -> false;
                                            in
                                                check (len1-1)
            else
                false

Using pure match everywhere

let check_cipher_2 s1 s2 = 
    let len1 = String.length s1 in
        let len2 = String.length s2 in
            match () with
                | () when len1 = len2 -> 
                        let rec check pos =
                            match pos with
                                | -1 -> true
                                | _ -> 
                                    let sub1 = s1.[pos] in
                                        let sub2 = s2.[pos] in
                                            (*http://stackoverflow.com/questions/257605/ocaml-match-expression-inside-another-one*)
                                            match sub1 with
                                                | 'A' -> (match sub2 with
                                                            |'C' -> check (pos-1)
                                                            | _ -> false)
                                                | 'B' -> (match sub2 with
                                                            |'A' -> check (pos-1)
                                                            | _ -> false)
                                                | 'C' -> (match sub2 with
                                                            |'D' -> check (pos-1)
                                                            | _ -> false)
                                                | 'D' -> (match sub2 with
                                                            |'B' -> check (pos-1)
                                                            | _ -> false)
                                                | _ -> false
                                                    in
                                                        check (len1-1)
                | () -> false

Ok. The above two solutions are similar.

I produced these two, because in here http://www.quora.com/OCaml/What-is-the-syntax-for-nested-IF-statements-in-OCaml, some people say that if else is not prefered.

This is essentially the first time I ever wrote a not-that-simple function in my whole life. So I am really hungry for suggestions here.

For exmaple,

  • how can I improve these solutions?
  • should I prefer match over if else?
  • Am I designing the rec or use the rec correctly?
  • if that in check (len1-1) correct?

Scale it

The exercise asks How does your code scale as the alphabet gets larger?. I really don’t have a clue for now. In Java, I would say I will have a map, then for each char in s1, I am looking s2 for the according char and to see whether it is the value in the map.

Any suggestions on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:58:46+00:00Added an answer on June 17, 2026 at 8:58 am

    The simplest solution seems to be to just cipher the text and compare the result:

    let cipher_char = function
       | 'A' -> 'C'
       | 'B' -> 'A'
       | 'C' -> 'D'
       | 'D' -> 'B'
       | _ -> failwith "cipher_char"
    let cipher = String.map cipher_char
    let check_cipher s1 s2 = (cipher s1 = s2)
    

    The cipher_char function scales linearly with the size of the alphabet. To make it a bit more compact and generic you could use a lookup table of some form, e.g.

    (* Assume that only letters are needed *)
    let cipher_mapping = "CADB"
    let cipher_char c =
       try cipher_mapping.[Char.code c - Char.code 'A']
       with Invalid_argument _ -> failwith "cipher_char"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Jason Hickey's Introduction to Objective Caml . Just have a question
I am learning Jason Hickey's Introduction to Objective Caml . Just have a question
I am learning Jason Hickey's Introduction to Objective Caml . Here is an exercise
I am learning Jason Hickey's Introduction to Objective Caml . After I learned Chapter
I am learning Jason Hickey's Introduction to Objective Caml . I got several simple
I am learning Jason Hickey's Introduction to Objective Caml . It says Matching against
I have two users Jason and postgres . Since I just started learning this,
This is a learning exercise in expression trees. I have this working code: class
I'm learning python and i loop like this the json converted to dictionary: it
I am learning Ruby on Rails. I made a simple link like this: <%=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.